iOS-通知传值

来源:互联网 发布:淘宝手动绞肉机 编辑:程序博客网 时间:2024/04/29 15:50

通知传值要分清楚发送方和接收方

发送方只需要发送通知 而接收方需要3步

1.注册通知

2.写出通知的回调方法

3.移除通知

如果接收通知的一方是用故事版做的 那么注册通知不能写在

viewDidLoad方法里面 而是在在页面将要显示的时候 用

- (instancetype)initWithCoder:(NSCoder *)aDecoder去拦截他的初始化方法 在里面注册通知

像这样

- (instancetype)initWithCoder:(NSCoder *)aDecoder

{

    self = [superinitWithCoder:aDecoder];

    if (self) {

        [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(shoudao:)name:@"tongzhi"object:nil];

    }

    returnself;

}


然后 发送方发送通知

参数1.通知的名字

参数2.传递的内容

 [[NSNotificationCenterdefaultCenter]postNotificationName:@"tongzhi"object:_game];

这样在通知的回调方法里面就可以接收到传递的内容 

-(void)shoudao:(NSNotification *)info{

    NSLog(@"00");

    NSLog(@"%@",info);

    [self.downLoadArraddObject:info.object];

    [self.tableViewreloadData];

    

}



可以看到info里面就保存了要传递的内容

最后再移除通知

-(void)dealloc{

    [[NSNotificationCenterdefaultCenter]removeObserver:self];

}



0 0
原创粉丝点击