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

  • 相关阅读:
    (萌O(∩_∩)O)哈希知识点小结
    hash应用以及vector的使用简介:POJ 3349 Snowflake Snow Snowflakes
    BestCoder Round #3HDU 4907
    搜索:POJ2251&POJ1426&POJ3087&POJ2488
    母函数初学四大题
    欧拉函数知识点总结及代码模板及欧拉函数表
    欧几里德与扩展欧几里德算法以及青蛙的约会~
    KMP 知识点总结
    HDU 1142 A Walk Through the Forest(dijkstra+记忆化DFS)
    HDU 1535 Invitation Cards(SPFA,及其优化)
  • 原文地址:https://www.cnblogs.com/naizui/p/5263330.html
Copyright © 2011-2022 走看看