• 消息通知机制(NSNotification和NSNotificationCenter)


    作者:FlyElephant
    出处:http://www.cnblogs.com/xiaofeixiang

    iOS中委托模式和消息机制基本上开发中用到的比较多,一般最开始页面传值通过委托实现的比较多,类之间的传值用到的比较多,不过委托相对来说只能是一对一,比如说页面A跳转到页面B,页面的B的值改变要映射到页面A,页面C的值改变也需要映射到页面A,那么就需要需要两个委托解决问题。NSNotificaiton则是一对多注册一个通知,之后回调很容易解决以上的问题。

    基础概念
    iOS消息通知机制算是同步的,观察者只要向消息中心注册, 即可接受其他对象发送来的消息,消息发送者和消息接受者两者可以互相一无所知,完全解耦。这种消息通知机制可以应用于任意时间和任何对象,观察者可以有多个,所以消息具有广播的性质,只是需要注意的是,观察者向消息中心注册以后,在不需要接受消息时需要向消息中心注销,属于典型的观察者模式。
    消息通知中重要的两个类:

    (1)NSNotificationCenter: 实现NSNotificationCenter的原理是一个观察者模式,获得NSNotificationCenter的方法只有一种,那就是[NSNotificationCenter defaultCenter] ,通过调用静态方法defaultCenter就可以获取这个通知中心的对象了。NSNotificationCenter是一个单例模式,而这个通知中心的对象会一直存在于一个应用的生命周期。

    (2) NSNotification: 这是消息携带的载体,通过它,可以把消息内容传递给观察者。

    实战演练
    1.通过NSNotificationCenter注册通知NSNotification,viewDidLoad中代码如下:

    1
    2
    3
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationFirst:) name:@"First" object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSecond:) name:@"Second" object:nil];
    第一个参数是观察者为本身,第二个参数表示消息回调的方法,第三个消息通知的名字,第四个为nil表示表示接受所有发送者的消息~

    回调方法:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    -(void)notificationFirst:(NSNotification *)notification{
    NSString *name=[notification name];
    NSString *object=[notification object];
    NSLog(@"名称:%@----对象:%@",name,object);
    }

    -(void)notificationSecond:(NSNotification *)notification{
    NSString *name=[notification name];
    NSString *object=[notification object];
    NSDictionary *dict=[notification userInfo];
    NSLog(@"名称:%@----对象:%@",name,object);
    NSLog(@"获取的值:%@",[dict objectForKey:@"key"]);
    }
    2.消息传递给观察者:

    1
    2
    3
    4
    5
    [[NSNotificationCenter defaultCenter] postNotificationName:@"First" object:@"博客园-Fly_Elephant"];

    NSDictionary *dict=[[NSDictionary alloc]initWithObjects:@[@"keso"] forKeys:@[@"key"]];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"Second" object:@"http://www.cnblogs.com/xiaofeixiang" userInfo:dict];
    3.页面跳转:

    1
    2
    3
    4
    -(void)pushController:(UIButton *)sender{
    ViewController *customController=[[ViewController alloc]init];
    [self.navigationController pushViewController:customController animated:YES];
    }
    4.销毁观察者

    1
    2
    3
    4
    -(void)dealloc{
    NSLog(@"观察者销毁了");
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
     也可以通过name单个删除:

    1
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"First" object:nil];
    5.运行结果

    1
    2
    3
    4
    2015-04-26 15:08:25.900 CustoAlterView[2169:148380] 观察者销毁了
    2015-04-26 15:08:29.222 CustoAlterView[2169:148380] 名称:First----对象:博客园-Fly_Elephant
    2015-04-26 15:08:29.222 CustoAlterView[2169:148380] 名称:Second----对象:http://www.cnblogs.com/xiaofeixiang
    2015-04-26 15:08:29.223 CustoAlterView[2169:148380] 获取的值:keso

  • 相关阅读:
    Cpp Singleton
    回溯法( Backtracking Algorithms ) :C语言Maze迷宫问题(自己实现)
    First wxWidgets Demo, wxWidgets简单示例
    copy contructor拷贝构造函数 (Copy Control)
    UPX (Ultimate Packer for eXecutables) 多平台可执行文件压缩
    wxWidgets Event Handling
    WxWidgets 安装与测试
    顺序查找 sequential find
    HDU1171 Big Event in HDU 背包
    HDU2091 水题
  • 原文地址:https://www.cnblogs.com/feiyu-mdm/p/8474629.html
Copyright © 2020-2023  润新知