有时候触发某个时间的时候想通知别的类进行相应的处理,这种情况可解决的方式很多。

比如继承、自定义delegate、通知等。

在此我说明下通知的运用。

 

事件A的触发,想让B做出相应的处理。

1、在B中注册通知:

 

 

[plain]  view plain copy
  1. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ButtonClick:) name:@"XXX" object:nil];  


 

2、在B中实现接收到通知的回调函数

 

 

[plain]  view plain copy
  1. - (void)ButtonClick:(NSNotification *)notification  
  2. {  
  3. }  


 

3、在A中事件触发时候实现post通知:

 

 

[plain]  view plain copy
  1. [[NSNotificationCenter defaultCenter] postNotificationName:@"XXX"   
  2.                                                                     object:self  
  3.                                                                   userInfo:nil];  


 

在userInfo参数中需要的话也可以传递一些信息,放在NSDictionary中传递过去,然后在B的ButtonClick函数里通过[notification userInfo]获取