NSNotification 通知
在发送通知的类中实现发送通知的行为,确定通知的名称,发送的内容等信息
CocoaLigature0 [[NSNotificationCenter defaultCenter] postNotificationName:@"WeatherAndPhoneUser" object:self userInfo:@{@"weather":@"Sunny"}];
确定了通知的名称为WeatherAndPhoneUser,发送的人为self,userInfo为发送内容,是一个字典CocoaLigature1
想要获取通知中心发出的信息,必须要实现以下几个步骤
1.在通知中心注册观察者(监听者)
2.实现接收到通知的动作(实现方法)
3.移除监听者(移除观察者)
1.首先在通知中心来注册观察者(监听通知)
CocoaLigature0 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction:) name:@"WeatherAndPhoneUser" object:nil];
这里的object是所要接收通知的对象,就是接收来自object这个对象的通知
注册观察者是在初始化函数中注册,因为这样一来,就可以一开始就可以接收通知
2.然后实现接收到通知的动作,实现该方法notificationAction
3.最后移除监听者(观察者)
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"weatherAndPhoneUser" object:nil];