zoukankan      html  css  js  c++  java
  • iOS多视图传值方式之通知传值(NSNotification;NSNotificationCenter)

    • iOS传值方式之5:通知传值

    • 第一需要发布的消息,再创建NSNotification通知对象,然后通过NSNotificationCenter通知中心发布消息(NSNotificationCenter单例)

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

    {

        // 创建需要传递的参数

        NSDictionary *dic = @{

                              @"name":@"coputer",

                              @"message":@"using"

                              };

        

        // 创建通知

        NSNotification *note = [NSNotification notificationWithName:@"tongzhi" object:nil userInfo:dic];

        

        // 通过通知中心发送消息:Xcode的单例

        [[NSNotificationCenter defaultCenter] postNotification:note];

        

        // 推到下一页:即接收通知的对象(使用的是模态视图)

        [self presentViewController:[PPFViewController new] animated:YES completion:nil];

    }


    • 在发送通知后,在所要接收的控制器中注册通知监听者,将通知发送的信息接收
    • 接收通知也有三步:首先需要注册通知,

    - (void)viewDidLoad

    {

        [super viewDidLoad];

          // 注册通知

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

    }

    // 反馈通知:打印通知内容

    - (void)reserve:(NSNotification *)messgae

    {

        NSLog(@"收到通知");

        NSLog(@"%@", messgae.userInfo);  //形参是NSNotification类型对象指针,因此可以访问其属性

    }

    // 移除通知

    - (void)dealloc

    {

    // ARC可以重写dealloc但不能写[super dealloc]; 因为是自动内存管理

        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"tongzhi" object:nil];

    }


    • 注意:

    其中,removeObserver:是删除通知中心保存的调度表一个观察者的所有入口,

    而removeObserver:name:object:是删除匹配了通知中心保存的调度表中观察者的一个入口。

    注意参数notificationObserver为要删除的观察者,一定不能置为nil。

  • 相关阅读:
    如何正确使用 Composer 安装 Laravel 扩展包
    sql之left join、right join、inner join的区别
    mysql decimal类型与decimal长度用法详解
    mysql数据库操作
    linux下如何查看某软件是否已安装
    MySQL数据类型和常用字段属性总结
    mysql数据库字段类型的选择原则
    linux shell 指令 诸如-d, -f, -e之类的判断表达式
    常用的Homebrew命令
    windows下安装php5.5的redis扩展
  • 原文地址:https://www.cnblogs.com/pruple/p/5281051.html
Copyright © 2011-2022 走看看