1.添加监听
- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;
sample:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomething:) name:GET_UP object:nil];
说明:
【NSNotificationCenter defaultCenter】消息中心只有一个,通过该方法获取该类的单例
addObserver:添加监听者
selector:表示监听到到对象后的处理函数
name:表示注册所关心的消息名称
2. 接收消息
- (void)postNotification:(NSNotification *)notification;
- (void)postNotificationName:(NSString *)aName object:(id)anObject;
- (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;
sample:
[[NSNotificationCenter defaultCenter] postNotificationName:GET_UP object:[NSNumber numberWithInteger:self.time]];
分析:
GET_UP:推送消息的名称
object:发送消息的对象
userInfo:发送消息携带的信息
3.删除监视
- (void)removeObserver:(id)observer;
- (void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject;