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

    通知 是在跳转控制器之间常用的传值代理方式,除了代理模式,通知更方便、便捷,一个简单的Demo实现通知的跳转传值.

     
     
     
    输入所要发送的信息 ,同时将label的值通过button方法调用传递,

    - (IBAction)buttonClick:(id)sender {

        //添加 字典,将label的值通过key值设置传递

        NSDictionary *dict =[[NSDictionary alloc]initWithObjectsAndKeys:self.textFieldOne.text,@"textOne",self.textFieldTwo.text,@"textTwo", nil];

        //创建通知

        NSNotification *notification =[NSNotification notificationWithName:@"tongzhi" object:niluserInfo:dict];

        //通过通知中心发送通知

        [[NSNotificationCenter defaultCenter] postNotification:notification];

        [self.navigationController popViewControllerAnimated:YES];

     

    }

    在发送通知后,在所要接收的控制器中注册通知监听者,将通知发送的信息接收

    - (void)viewDidLoad {

        [super viewDidLoad];

        //注册通知

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

     

    }

    - (void)tongzhi:(NSNotification *)text{

        NSLog(@"%@",text.userInfo[@"textOne"]);

            NSLog(@"-----接收到通知------");

     

    }

    移除通知:removeObserver:和removeObserver:name:object:

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

    这个比较简单,直接调用该方法就行。例如:

    [[NSNotificationCenter defaultCenter] removeObserver:observer name:nil object:self];

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

    ==================================================不华丽的分割线========================================

    以上是网上摘抄...自己学习的.虽然我也没太看明白.但是我自己也写出来了.看着方便如下.

    在发送通知的地方:

       [[NSNotificationCenter defaultCenter] postNotificationName:@"PostCode" object:XXX];

    我就这一行...不知道为什么网上都好长好长.....XXX是要传出去的值

    然后接收的地方:

     //接收code 通知

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

    这里我也不知道为什么后面的object都为nil了.还有值..不管.反正我一直这么写.

    //收到通知后事件

    - (void)goToBoundPhone:(NSNotification *)goToBoundPhone{

        _code = goToBoundPhone.object;

    }

    让明天,不后悔今天的所作所为
  • 相关阅读:
    奥数视频
    提车应该检查哪?4S店都怕你检查这4个“雷区”,别等后悔才知道
    乒乓球拍子和套胶选择
    2018天津英华国际学校初中报名指南
    水瓶座出生日期是几月几号到几月几号
    乒乓球 世锦赛
    鸡蛋羹要怎么蒸才会更嫩?秘诀在这里
    家庭理财方法:知道这7个定律可以帮你赚更多钱!
    要知道股市有“三底”,估值底、政策底、市场底!
    DbMigration使用方法
  • 原文地址:https://www.cnblogs.com/-yun/p/4708380.html
Copyright © 2011-2022 走看看