iOS利用通知逆传值

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

 

#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];

}

 结果:

 

 

posted @ 2016-08-22 23:31  厚积薄发1  阅读(324)  评论(0)    收藏  举报