zoukankan      html  css  js  c++  java
  • Notification传值

    接触通知中心很久了,一直也没用过它传值,今天任务完成的比较早,所以做了个小demo,后界面通知中心往前界面传值

    发送者视图控制器:

    - (void)viewDidLoad {

        [super viewDidLoad];

    }

    - (IBAction)call:(id)sender {

        //当输入框内的文字不为空时

        if (_aTextfield.text) {

            //以输入框的值作为value,以A作为键,存储为字典

            NSDictionary *dics = @{@"A":_aTextfield.text};

            //创建notification对象,name名字为两个控制器通知的唯一标识

            NSNotification *notifications = [[NSNotification alloc] initWithName:@"abc" object:nil userInfo:dics];

            [[NSNotificationCenter defaultCenter]postNotification:notifications];

            [self dismissViewControllerAnimated:YES completion:nil];

        }

    }

     接受者视图控制器:

    - (void)viewDidLoad {

        [super viewDidLoad];

        //接受者注册通知中心,name字段和发送者相同

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

                                            

    }

    -(void)abc:(NSNotification *)notif {

       _abt.text = notif.userInfo[@"A"];

        NSLog(@"%@",_abt.text);

        

    }

    注意:dalloc写在创建观察者的控制器里面

    - (void)dealloc

    {

        //消息发送完,要移除掉

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

    }

    总结:综上所述,通知中心在两个视图控制器关联很少的时候很适合使用,轻量级传值,堪比属性传值

  • 相关阅读:
    通过 VB5 创建 ActiveX DLL 文件并用 ASP 调用一例
    Autocad VBA初级教程
    自学资料第一集
    Linux虚拟化:10个不得不爱的理由
    EXCEL VBA编程的一些小结
    FAQ 工作薄及工作表
    很重要的EXCEL使用技巧
    Excel VBA编程的常用代码
    VBA生成一个CorelDraw工具栏
    支付宝,网银在线,快钱 3大支付接口的集成与对比,统合实现
  • 原文地址:https://www.cnblogs.com/OC888/p/5515004.html
Copyright © 2011-2022 走看看