iOS开发之自定义通知

来源:互联网 发布:淘宝直通车开通流程 编辑:程序博客网 时间:2024/04/30 17:59

使用步骤:

1、在某一方法中发送出一个通知


- (void)someFun{        [[NSNotificationCenter defaultCenter] postNotificationName:@"receiveBack" object:self];        //此方法含义:当前对象发送一个receiveBack的通知        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"receiveBack" object:self userInfo:dic];                //此方法含义:当前对象发送一个receiveBack的通知,并传递一个dic参数
 }


2、在某个方法中实现对通知的监听
- (void)someFun {
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(callBack) name: @"back" object: nil];
    //此方法含义:当前对象接收一个名为receiveBack的通知,不管这个通知是谁发送的(因为object这个参数为nil),并且回调这callBack方法
}

3、在回调方法中处理业务逻辑
- (void)callBack{    NSLog(@"i am back.");}

4、在dealloc方法中移除通知

- (void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

0 0
原创粉丝点击