码迷,mamicode.com
首页 > 移动开发 > 详细

iOS利用通知逆传值

时间:2016-08-23 01:10:38      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

 直接创建两个控制器,点击跳转第二个界面,然后点击按钮进行传值

 

#import "ViewController.h"

#import "TWOOViewController.h"

@interface ViewController ()

@property(nonatomic,weak)UILabel * label;

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    UIButton * nextBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width, 50)];

    nextBtn.backgroundColor = [UIColor redColor];

    [nextBtn addTarget:self action:@selector(nextBtnClick:) forControlEvents:UIControlEventTouchUpInside];

    [nextBtn setTitle:@"下一个界面" forState:UIControlStateNormal];

    

    [self.view addSubview:nextBtn];

    

    

    

    UILabel * labeltext = [[UILabel  alloc]initWithFrame:CGRectMake(0, 150, self.view.bounds.size.width, 50)];

    labeltext.backgroundColor = [UIColor yellowColor];

    labeltext.textColor = [UIColor greenColor];

    [self.view addSubview:labeltext];

    self.label = labeltext;

    //创建通知

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(nexttow:) name:@"nextBtnaa" object:nil];

    

    

}

-(void)nextBtnClick:(UIButton*)nextBtn{

     TWOOViewController * two = [[TWOOViewController alloc]init];

    [self presentViewController:two animated:YES completion:nil];

 

}

 

-(void)nexttow:(NSNotification*)nontification{

    self.label.text = nontification.object;

 

 

}

 第二个控制器

#import "TWOOViewController.h"

 

@interface TWOOViewController ()

 

@end

 

@implementation TWOOViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor blueColor];

    UIButton  * backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 79, self.view.bounds.size.width, 50)];

    

    [backBtn setTitle:@"回" forState:UIControlStateNormal];

    [backBtn addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside];

    

    [self.view addSubview:backBtn];

    

 

}

 

-(void)backBtnClick:(UIButton*)backBtn{

    

    [[NSNotificationCenter defaultCenter]postNotificationName:@"nextBtnaa" object:@"逆传值"];

    

    //隐藏本控制器

    [self dismissViewControllerAnimated:YES completion:nil];

}

 结果:

技术分享

 技术分享

技术分享

 

iOS利用通知逆传值

标签:

原文地址:http://www.cnblogs.com/hb201411/p/5797650.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!