zoukankan      html  css  js  c++  java
  • NSNotification --关于通知

    一、

         通知(NSNotification)的发送(Post)和注册(add)都必须借助于通知中心(NSNotificationCenter)

         发送通知:

     NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center postNotificationName:@"通知名字" object:self userInfo:@{@"key":@"object"}];

         发送的通知中:

    1.NotificationName是这则通知的名字,

    2.object通常写self(可以在通知的接受者中提取到这个,实现回调,)

    3.userInfo是一个字典用来存放传输的数据

         注册通知接受者:(第一种方法)

       [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change:) name:@"通知名字" object:nil];

    //接受所有名字是"通知名字"的通知

        注册通知接受着:(第二种方法)

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

      只接受object:后面带参数的人所发出的通知    

    以下两端是从 http://blog.csdn.net/zhanglei5415/article/details/6454940 摘抄的代码和解释

    - (void)addObserver:(id)anObserver
               selector:(SEL)aSelector
                   name:(NSString *)notificationName
                 object:(id)anObject

    注册anObserver对象:接受名字为notificationName, 发送者为anObject的notification. 当anObject发送名字为notificationName的notification时, 将会调用anObserver的aSelector方法,参数为该notification. 如果notificationName为nil. 那么notification center将anObject发送的所有notification转发给observer
    . 如果anObject为nil.那么notification center将所有名字为notificationName的notification转发给observer

    那么有通知的注册(add),就必须有通知的销毁(remove),下文中很好的提到了销毁时的注意事项等

    http://www.jianshu.com/p/a4d519e4e0d5

  • 相关阅读:
    vue3.0 sync属性变化
    webRTC技术
    Vue3不支持eventBus
    远程连接MySQL数据库报错:is not allowed to connect to this MYSQL server的解决办法
    基于node.js实现前端web项目自动化部署
    SH 远程连接 Windows 服务器
    七牛云使用之配置域名CNAME
    基于python win32setpixel api 实现计算机图形学相关操作
    [JavaScript闭包]Javascript闭包的判别,作用和示例
    将exe程序添加到服务的命令
  • 原文地址:https://www.cnblogs.com/naizui/p/5263330.html
Copyright © 2011-2022 走看看