NSNotificationCenter 通知机制

在cocoa透彻 框架中,观察者模式的具体应用有两个通知机制和kvo机制

1.通知机制  与委托机制不同的是,前者是“一对多”的对象 之间的通信,后者是“一对一”的对象之间的通信。

通知机制的可以1对多个控制器,1个控制器监听,其他控制器发出请求 。

监听

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleEnterForeground:)
                                                 name:UIApplicationWillEnterForegroundNotification
                                               object:nil];

发出请求

 [[NSNotificationCenter defaultCenter]
                postNotificationName:@"RegisterCompletionNotification"
                object:nil
                userInfo:dataDict];

 

发出请求后 由于 postNotificationName 是相同的, 在另外的一个控制器就能 监听到发出的请求。

posted @ 2015-11-09 17:13  Lee_M  阅读(167)  评论(0编辑  收藏  举报