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。

  • 相关阅读:
    TabActivity 、fragemnt+fragment 和 Fragment+Viewpager 优缺点
    Android App的签名打包(晋级篇)
    Android ProgressBar 进度条荧光效果
    Android UI【android 仿微信、QQ聊天,带表情,可翻页,带翻页拖动缓冲】
    Android UI--自定义ListView(实现下拉刷新+加载更多)
    Android平台下利用zxing实现二维码开发
    自定义组件---图片和文字实现ImageButton效果
    JNI由浅入深_10_JNI 综合开发
    开关按钮实现
    Android Asynctask与Handler的比较,优缺点区别,Asynctask源码
  • 原文地址:https://www.cnblogs.com/pruple/p/5281051.html
Copyright © 2011-2022 走看看