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

原文地址:https://www.cnblogs.com/naizui/p/5263330.html