NOtifition (通知中心)
是成對出現(xiàn)的 : 有注冊 就有釋放
在 dealloc 方法中進行釋放
1.Notifition 的規(guī)范
在 .h 文件中
import <UIKit/UIKit.h>
//通知名定義
extern NSString *const kNotifition;
在 .m 文件中
import "ViewController.h"
NSString *const kStartNotifition = @"kStartNotifition";
注意:每一個程序都有一個自己的通知中心,即NSNotificationCenter對象。該對象采用單例設(shè)計模式,采用defaultCenter方法就可以獲得唯一的NSNotificationCenter對象。
2.注冊通知:addObserver:selector:name:object:
//注冊通知NSNotificationCenter
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor) name:@"color" object:nil];
3.發(fā)送通知:postNotificationName:object:或者performSelectorOnMainThread:withObject:waitUntilDone:
//發(fā)送消息
[[NSNotificationCenter defaultCenter] postNotificationName:@"color" object:nil];
object: 后可以用來 傳值,(所謂的通知傳值)
通知都是唯一的,通過 name: 來區(qū)分是哪個通知
4.移除通知:removeObserver:和removeObserver:name:object:
[[NSNotificationCenter defaultCenter] removeObserver:observer name:@"color" object:self];
在dealloc方法中移除