通知


场景:一般为model层对,controller和view进行的通知方式,不关心谁去接收,只负责发布信息。

特点:一对多发布消息;

功能:既可传值又可传对象

注意:

        1. 使用后要在delloc中释放;

        2. 在传字典的时候需注意,字典中不能定义变量;

        3.  addNotification  要放在 PostNotifacation 提前申明;

具体用法:

        // 第一步:发布通知消息

 [[NSNotificationCenter defaultCenter] postNotificationName:@"tongzhi" object:nil userInfo:nil];

     

     //第二步:在需要的地方添加观察者用来接收消息

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


    

     -(void)action:(NSNotification *)noti

    {

    NSDictionary *dic=noti.userInfo;

    NSLog(@"------------%@-------%@",dic[@"name"],dic[@"age"]);

     Person *pson= (Person *)noti.object;

    NSLog(@"-----------%@--------%d",pson.name,pson.age);

    }


    //第三步:释放消息

    -(void)dealloc

    {

    [[NSNotificationCenter defaultCenter]  removeObserver:self name:@"tongzhi" object:nil];

    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值