//注册通知
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(click:)name:@"mynotification"object:nil];
UIButton *jumpBtn = [[UIButtonalloc]initWithFrame:CGRectMake(50,100, 200, 50)];
jumpBtn.backgroundColor = [UIColorredColor];
[jumpBtn setTitle:@"click..."forState:UIControlStateNormal];
[jumpBtn addTarget:selfaction:@selector(clickBtn)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:jumpBtn];
------------------------------------------------------
-(void)click:(NSNotification*)text{
NSString *str = text.userInfo[@"1"];
NSLog(@"---------------%@",str);
}
-(void)clickBtn{
//PS:UIViewController 跳转方法有两种,1.利用UIViewController自身的presentModalViewController,进行跳转;调用dismissModalViewControllerAnimated方法可以返回。2.利用UINavigationController,调用pushViewController,进行跳转;这种采用压栈和出栈的方式,进行Controller的管理。调用popViewControllerAnimated方法可以返回.
TwoViewController *vc = [[TwoViewControlleralloc]init];
[selfpresentViewController:vc animated:YEScompletion:nil];
// Or [self.navigationController pushViewController:vc animated:YES];
}
NSDictionary *dic = [[NSDictionaryalloc]initWithObjectsAndKeys:@"this",@"1",@"is",@"2",@"a",@"3",@"dictionary",@"4",nil];
//创建通知
NSNotification *myNotification = [NSNotificationnotificationWithName:@"mynotification"object:nil userInfo:dic];
//通过通知中心发送通知
[[NSNotificationCenterdefaultCenter]postNotification:myNotification];
移除通知:removeObserver:和removeObserver:name:object:
其中,removeObserver:是删除通知中心保存的调度表一个观察者的所有入口,而removeObserver:name:object:是删除匹配了通知中心保存的调度表中观察者的一个入口。
这个比较简单,直接调用该方法就行。例如:
[[NSNotificationCenter defaultCenter] removeObserver:observer name:nil object:self];
注意参数notificationObserver为要删除的观察者,一定不能置为nil。