通知传值要分清楚发送方和接收方
发送方只需要发送通知 而接收方需要3步
1.注册通知
2.写出通知的回调方法
3.移除通知
如果接收通知的一方是用故事版做的 那么注册通知不能写在
viewDidLoad方法里面 而是在在页面将要显示的时候 用
- (instancetype)initWithCoder:(NSCoder *)aDecoder去拦截他的初始化方法 在里面注册通知
像这样
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(shoudao:) name:@"tongzhi" object:nil];
}
return self;
}
参数1.通知的名字
参数2.传递的内容
[[NSNotificationCenter defaultCenter]postNotificationName:@"tongzhi" object:_game];
这样在通知的回调方法里面就可以接收到传递的内容
-(void)shoudao:(NSNotification *)info{
NSLog(@"00");
NSLog(@"%@",info);
[self.downLoadArr addObject:info.object];
[self.tableView reloadData];
}
可以看到info里面就保存了要传递的内容
最后再移除通知
-(void)dealloc{
[[NSNotificationCenter defaultCenter]removeObserver:self];
}