NSNotification--通知传值
NSNotification
最近做练习项目发现在一个没有控制器的UIView下puch到UIViewcontroller;需要用到通知传值
通知传值是一对多的传值方法;
//添加 字典,将label的值通过key值设置传递
NSDictionary *dict =[[NSDictionary alloc] initWithObjectsAndKeys:[NSString stringWithFormat:@"%d",(int)aBtn.tag],@"aBtn.tag", nil];
//创建通知
NSNotification *notification =[NSNotification notificationWithName:@"PUCH" object:nil userInfo:dict];
//通过通知中心发送通知
[[NSNotificationCenter defaultCenter] postNotification:notification];
移除通知:
[[NSNotificationCenter defaultCenter] removeObserver:observer name:nil object:self];
在需要的界面:
//注册通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tongzhi:) name:@"PUCH" object:nil];
- (void)tongzhi:(NSNotification *)puch{
NSLog(@"%@",puch.userInfo[@"aBtn.tag"]);
NSLog(@"-----接收到通知------");}