猿起码农之亮眼

NSNotificationCenter带参数的通知

2016-08-19  本文已影响87人  飞天猪Pony

下面以字符串为例,数组字典传递方式都是一样。
NSString *string = @"要传的参数字符串";

传递参数:
[[NSNotificationCenter defaultCenter] postNotificationName:@"NOITIFYNAME" object:string];
参数说明:

postNotificationName:通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。
object:传递的参数

接收参数:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recieveParameter:) name:@"NOITIFYNAME" object:nil];
- (void)recieveParameter:(NSNotification *)aNotification
{
      NSString *aString = [aNotification object]; 
}
参数说明:

aString就是接收到的参数

- (void)dealloc
{
      [[NSNotificationCenter defaultCenter] removeObserver:self name:"NOITIFYNAME" object:nil]; 
}
上一篇 下一篇

猜你喜欢

热点阅读