NSNotificationCenter(通知中心)
【注意】需再dealloc中移除觀察者
獲取通知中心單例對(duì)象
NSNotificationCenter *center=[NSNotificationCenter defaultCenter];
常用方法:
1.注冊(cè)觀察者
- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;
參數(shù):
observer: 觀察者
aSelector: 收到通知會(huì)自動(dòng)調(diào)用此方法
aName: 消息內(nèi)容(接收通知的"暗號(hào)")
anObject: 一般為nil
2.發(fā)送通知消息
- (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;
參數(shù):
aName: 誰(shuí)注冊(cè)了通知中心,觀察者模式設(shè)置這個(gè)消息內(nèi)容,便通知誰(shuí)
anObject: 傳送的參數(shù)(任意對(duì)象)
aUserInfo: 傳送的字典
NSNotification(若被通知者調(diào)用的方法中帶有參數(shù),則傳遞此對(duì)象過(guò)來(lái))
常用方法:
1.獲取傳遞的參數(shù)
- (id)object;
2.獲取傳遞的字典
- (NSDictionary *)userInfo;