20121104(002)Notification App 通知消息的使用

目标:完成简单消息对象之间的传递

原理:

      1、在通知中心(一个单例)注册观察者(接收者)

      2、通过通知中心发送数据

      3、观察者接收数据并处理数据

      4、用完通知后移除观察者

第一步:

注册观察者

- (void)registerObserver
{
    //通过NotificationCenter注册一个观察者
    observerViewController = [[ObserverViewController alloc]init];
    [NSNotificationCenter defaultCenter] addObserver:observerViewController selector:@selector(handNotification:) name:@"BNRNOTIFICATION" object:nil];
}

第二步:

发送通知给通知中心

- (IBAction)sendNotification:(id)sender
{
    NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"value1",@"key1",@"value2",@"key1",nil];
[   [NSNotificationCenter defaultCenter] postNotificationName:@"BNRNOTIFICATION" object:nil userInfo:userInfo];
}
第三步:

观察者处理通知中心发过来的通知消息

- (void)handNotification:(NSNotification *)notify
{
    NSLog(@"handNotification");
    NSDictionary *dict = [notify userInfo];
    NSLog(@"userInfo description = %@",[dict description]);
}

第四步:

在通知中心移除观察者

- (IBAction)removeNotification:(id)sender
{
    [[NSNotificationCenter defaultCenter] removeObserver:observerViewController name:@"BNRNOTIFICATION" object:nil];
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值