通知的机制是一对多,而block和delegate的机制是一对一,通知是好用,但小伙伴么要记得通知比较耗性能哦~~~

谁要发送消息,谁就发出通知,谁要接受消息,谁就销毁通知.

下面直接来看代码:

//发出通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"notification" object:image];
 
 
//谁接受通知 谁就销毁通知
- (void)viewDidLoad {
    [super viewDidLoad];
   
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(valueChanged:) name:@"notification" object:nil];
   
}

- (void)valueChanged:(NSNotification *)notification{
  // object 就是传过来的参数
    self.imgView.image = notification.object;
   
}

// 在对象销毁的时候移除通知
    -(void)dealloc{
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }