[ios]自定义Notifacion

1.通知者

想发送通知消失的时候

NSString * test=@"mytest";

  NSDictionary * notifyObject= [NSDictionary dictionaryWithObjectsAndKeys:test,@"test",nil];

 [[NSNotificationCenter defaultCenter] postNotificationName: @"myNotification" object:mynotifyObject];

这里notifyObject为参数要字典形式,如果没有参数可写nil

myNotification 为通知者名称(id) 通过这个区别于其他的通知中心,一般定义为全局的变量。

2.接收通知者

先注册一个通知中心(一般写在viewdidload里), 名称和发送的通知中心要保持一致

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mynotificationFunc:) name:@"myNotification" object:nil];

接收通知后系统会自己调用mynotificationFunc: 方法

-(void) notificationFunc:(NSNotification *)nt{

//接收参数

NSDictionary * dic=(NSDictionary*)[nt object];

NSString *test=[dic objectForKey:@"test"];

NSlog(@"%@",test);

}

很简单。。。

原文地址:https://www.cnblogs.com/jinjiantong/p/2961100.html