通知的简单实用

 //添加消息中新的观察者

    /*

     1.观察者的对象指针

     2.接受到通知以后触发的方法 该方法必须带有参数

     3.消息的名字 一定要和发送的消息的名字完全小童

     4.nil//上下文

     */

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNotification:) name:@"changeColor" object:nil];

 ============实现方法===============

-(void)receiveNotification:(NSNotification *)notif

{

    // 参数的类型是固定的 NSNotification*

    // 可以通过参数 获取消息携带的内容

    UIColor *color = [notif object];

    self.view.backgroundColor = color;    

}

============发送通知==================

 

 //<1>建立通知中心 发送通知

    //postNotificationName:标示发送消息 第一个参数表示发送小时的名字(唯一)

    //第二参数是消息携带的内容(任意对象的指针 也可以是nil

    [[NSNotificationCenter defaultCenter]postNotificationName:@"changeColor" object:[UIColor purpleColor]];

posted @ 2015-08-23 22:55  BN笨的很想飞  阅读(155)  评论(0)    收藏  举报