zoukankan      html  css  js  c++  java
  • iOS NSNotification传递带参数的通知

    普通的通知使用
    注册观察者

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

    发送通知

    [[NSNotificationCenter defaultCenter] 
    postNotificationName:@"ThisIsANoticafication" object:nil];
    

    传递带参数的通知
    在发送通知时设置object参数

    [[NSNotificationCenter defaultCenter] 
    postNotificationName:@"ThisIsANoticafication" object:@{@"parameter1":@"1",@"parameter2":@"2"}];
    

    这里传入了一个字典,那么如何在接收通知的时候得到这个字典呢

    [[NSNotificationCenter defaultCenter] 
    addObserver:self selector:@selector(getNotificationAction:) name:@"ThisIsANoticafication" object:nil];
    
    - (void)getNotificationAction:(NSNotification *)notification{
        NSDictionary * infoDic = [notification object];
        // 这样就得到了我们在发送通知时候传入的字典了
    }
    

    当然传入的参数可以是其他类型。

  • 相关阅读:
    Caesar cipher
    遗传算法之背包问题
    Transport scheme NOT recognized: [stomp]
    error running git
    Canvas 旋转的图片
    canvas时钟
    火箭起飞
    让图标转起来
    Tomcat启动脚本
    Task中的异常处理
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/11190053.html
Copyright © 2011-2022 走看看