这是一个观察者模式。
首先在你需要监听的类中加入观察者:- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;
这个观察者在监听到anObject发送名字为aName的notification时,调用selector的方法,在aSelector方法中得到userInfo。
anObject表示从谁那儿发送出来的消息。
比如:
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(onWebClose)name:@"WebClose"object:nil];
也就是说监听到了object:nil发出消息,消息的名字是WebClose,此时observer就调用onWebClose方法。
如果object:nil表示以广播方式发消息或者得到消息,这个时候只要消息名字是对的就可以得到这个消息。
然后在被监听的类中发送通知:
[[NSNotificationCenterdefaultCenter] postNotificationName:@"WebClose"object:nil];
这样观察者就接到了消息会调用selector方法;最后记得移除这个观察者:
[[NSNotificationCenterdefaultCenter] removeObject:self];
这个的好处是:完全两个不相干的view可以建立起来联系;