iOS 消息通知

简单写了一个通知的demo,通过以下代码可以简单了解一下

我新建了3个UIViewController,分别命名为FirstViewController , SecondViewController , ThirdViewController
并在AppDelegate里设FirstViewController为根视图

FirstViewController

//FirstViewController.m
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor yellowColor];
    button.frame = CGRectMake(100, 100, 100, 100);
    [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

    //注册通知
    //参数1:添加当前对象为注册对象
    //参数2:设置收到消息后触发的方法
    //参数3:设置消息通知的标示符名字
    //参数4:设置接收对象的参数值,设置为nil
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeColor:) name:@"changeColor" object:nil];

}
//通知的的触发方法
- (void)changeColor:(NSNotification *)notification{

    //获得发送的消息传递过来的值
    UIColor *color = [notification object];
    self.view.backgroundColor = color;
}
//按钮点击触发方法,跳到SecondViewController
- (void)action:(UIButton *)btn{
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:secondVC animated:YES];
}

SecondViewController

//SecondViewController.m
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor yellowColor];
    button.frame = CGRectMake(100, 100, 100, 100);
    [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

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

}

- (void)changeColor:(NSNotification *)notification{

    //获得发送的消息传递过来的值
    UIColor *color = [notification object];
    self.view.backgroundColor = color;
}


- (void)action:(UIButton *)btn{
    ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
    [self.navigationController pushViewController:thirdVC animated:YES];
}

ThirdViewController

//ThirdViewController.m
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor whiteColor];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor yellowColor];
    button.frame = CGRectMake(100, 100, 100, 100);
    [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

}

- (void)action:(UIButton *)btn{

    //发送一个消息通知
    //参数2:发送消息时传递的值
    [[NSNotificationCenter defaultCenter]postNotificationName:@"changeColor" object:[UIColor brownColor]];

}

当点击ThirdViewController的按钮后,再返回前两页就可以发现,前两页的背景颜色变成了通知所发送的颜色了.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值