使用很简单的样子:
首先需要,在需要收听通知的地方注册一个通知
//我向通知中心注册了一条通知“ChangeLabelTextNotification”
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLabelText:) name:@"ChangeLabelTextNotification" object:nil];
注意 方法接收的参数类型是NSNotification
-(void)changeLabelText:(NSNotification *)notification
{
id text=notification.object;
UILabel *label=(UILabel *)[self.view viewWithTag:102];
label.text=text;
}
post通知:
#pragma mark -private
-(void)dismiss
{
//改变label的值
[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeLabelTextNotification" object:_textField.text];
/*if ([self.delegate respondsToSelector:@selector(changeLabelText:)]) {
[self.delegate changeLabelText:_textField.text];
}*/
if ([[UIDevice currentDevice].systemVersion floatValue]<6.0) {
//[self dismissModalViewControllerAnimated:YES];
} else {
[self dismissViewControllerAnimated:YES completion:^{
}];
}
}
当程序退出的时候,通知中心要移除掉:
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:kChangeLabelTextNotification object:nil];
[super dealloc];
}