1.NSNotification:消息或通知
有三個成員變量
- (NSString *)name;
- (id)object;
- (NSDictionary *)userInfo;
通知名稱:name,
消息發送者:object,代理在收到NSNotification方法裏,可以回調到object
附加信息:userInfo
2.NSNotificationCenter:消息中心
單例模式,需要通過以下類方法訪問
[NSNotificationCenter defaultCenter]
3.廣播一個通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"aEvent" object:nil];
發送一個附帶信息的通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"aEvent" object:nil userInfo:(NSDictionary *)aUserInfo];
aUserInfo是一個字典
直接發送一個通知
[[NSNotificationCenter defaultCenter] postNotification:(NSNotification *)notification]];
4.註冊一個通知監聽
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onHandler:) name:@"aEvent" object:nil];
註冊通知時,可以指定一個具體的廣播者對象object,但不是必須的
5.移除一個監聽
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"aEvent" object:nil];
6.通知要調用的方法
- (void)onHandler:(NSNotification *)notif
{
NSDictionary *user_info = [notif userInfo];
//執行自定邏輯
}