NSNotification屬于單例模式的一種
什么是單例模式:程序在編譯時會被實(shí)例化一次卡辰,提供一個類方法全局調(diào)用霹疫,會放在內(nèi)存中一直占有內(nèi)存拱绑,當(dāng)APP退出時由系統(tǒng)釋放內(nèi)存
添加通知,發(fā)送通知丽蝎,移除通知
1猎拨、發(fā)送通知 [[NSNotificationCenterdefaultCenter] postNotificationName:@"test0"object:_obj0 userInfo:@{@"key":@"_obj0"}];
2、添加通知?[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(respondsToNotification:) name:@"test0"object:_obj0];
3屠阻、移除通知
?//移除該響應(yīng)者的全部通知[[NSNotificationCenterdefaultCenter] removeObserver:self];//移除該響應(yīng)者 name==@"test0" 的全部通知[[NSNotificationCenterdefaultCenter] removeObserver:selfname:@"test0"object:nil];//移除該響應(yīng)者 name==@"test0" 且 object==_obj0 的全部通知[[NSNotificationCenterdefaultCenter] removeObserver:selfname:@"test0"object:_obj0];
關(guān)于object參數(shù)理解迟几,當(dāng)添加通知時指定了object,則只會接收到發(fā)送通知帶有object的栏笆,若發(fā)送通知時添加了object类腮,添加通知時object參數(shù)為nil,響應(yīng)者也能收到通知