NSNotification

来源:互联网 发布:js onclick没反应 编辑:程序博客网 时间:2024/05/21 21:48

NSNotification
解释:消息
一个类执行另一个类的方法时使用
注册消息
[[NSNotificationCenter defaultCenter] //默认通知对象
addObserver:self /观察者,即在什么地方接收通知
selector:@selector(realizeNotificationButton) //收到通知后调用何种方法
name:@“mytest” //通知的唯一标示,编译器通过这个找到通知
object:nil
];
在另一个类里调用消息
[[NSNotificationCenter defaultCenter]
postNotificationName:@”mytest” object:nil];
相应消息
-(void)realizeNotificationButton
{
NSLog(@”消息”);
}
备注:
若在第二步调用消息时传递了参数(object)
注册消息,有冒号
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleFirstNotification:) name:@”first” object:nil];

在另一个类里调用时object候跟参数
[[NSNotificationCenter defaultCenter] postNotificationName:@”mytest” object:self.TextFile.text];
相应消息时
- (void)handleFirstNotification:(NSNotification *)notification
{
NSString str = (NSString )notification.object;接收参数
}

1 0
原创粉丝点击