接收通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeMyLabel:) name:@"change" object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"change" object:message];
NSNotification* s = (NSNotification*)sender;
NSString* newMessage = [s object];
self.myLabel.text = newMessage;
发送通知
- (void)changeFirstView {
NSString* message = @"今天中午吃饺子";[[NSNotificationCenter defaultCenter] postNotificationName:@"change" object:message];
}
带参数的通知接收参数的方法
-(void)changeMyLabel:(id)sender
{NSNotification* s = (NSNotification*)sender;
NSString* newMessage = [s object];
self.myLabel.text = newMessage;
}
通知使用后需要移除,不然会占用内存,移除通知的方法
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"change" object:nil];
}