您的位置:首页 > 移动开发 > IOS开发

IOS通知中心(NSNotificationCenter)

2016-12-30 14:38 162 查看
通知中心(NSNotificationCenter)实际是在程序内部提供了一种广播机制。把接收到的消息,根据内部的消息转发表,将消息转发给需要的对象。这句话其实已经很明显的告诉我们要如何使用通知了。第一步:在需要的地方注册要观察的通知,第二步:在某地方发送通知。(这里注意:发送的通知可能是我们自定义的,也可能是系统的)。d

//注意,通知的使用是有先后顺序的
//一定要先监听通知,然后在发送通知


1、NSNotification


 这个类可以理解为一个消息对象,其中有三个成员变量。这个成员变量是这个消息对象的唯一标识,用于辨别消息对象。

 @property (readonly, copy) NSString *name;

 这个成员变量定义一个对象,可以理解为针对某一个对象的消息。

@property (readonly, retain) id object;

 这个成员变量是一个字典,可以用其来进行传值。

 @property (readonly, copy) NSDictionary *userInfo;

readonly 都是只读的,不能初始化

 NSNotification的初始化方法:

- (instancetype)initWithName:(NSString *)name object:(id)object userInfo:(NSDictionary *)userInfo;

+ (instancetype)notificationWithName:(NSString *)aName object:(id)anObject;

+ (instancetype)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary*)aUserInfo;

注意:官方文档有明确的说明,不可以使用init进行初始化


2、NSNotificationCenter

这个类是一个通知中心,使用单例设计,每个应用程序都会有一个默认的通知中心。就是用来接收消息的

添加一个观察者,可以为它指定一个方法,名字和对象。接受到通知时,执行方法。

- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;

发送通知消息的方法

- (void)postNotification:(NSNotification *)notification;

- (void)postNotificationName:(NSString *)aName object:(id)anObject;

- (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;

移除观察者的方法

- (void)removeObserver:(id)observer;

- (void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject;

使用====================

//1、获取通知中心,注册一个观察者和事件

    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

    //在通知中心添加一个观察者和一个观察的事件

    //参数1.负责响应事件的观察对象

    //参数2.接收到消息,观察者执行的方法

    //参数3.观察者要监听的事件

    //参数4.可以限定消息的发送者,如果为空就表名接收任何消息

    [center addObserver:self selector:@selector(receiveNotification:) name:@"ChangeColor" object:nil];

移除观察者

 //只要注册一个观察者,一定要在dealloc方法中移除掉自己观察者身份,ARC中也要这么做,切记!!!

    [[NSNotificationCenter defaultCenter] removeObserver:self];

//收到通知中心的消息时,观察者(self)要调用的方法

- (void)receiveNotification:(NSNotification *)message{

    NSLog(@"zzz®%@", message.name);

    NSLog(@"rrr%@", message.object);

    

}

//向通知中心发送一个消息

    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

    NSNotification *notice =        [NSNotification notificationWithName:@"ChangeColor"object:@"good" userInfo:nil];

    //参数1.发送消息的事件

    //参数2.可以使用这个参数传递一个对象给观察者

    //参数3.一些消息的参数信息(系统用的比较多)

    [center postNotification:notice];

=======================================

第2种通知的使用使用 block 比较方便简单

这个方法需要一个id类型的值接受

@property (nonatomic, weak) id observe;


 

//第二种方法
//Name: 通知的名称
//object:谁发出的通知
//queue: 队列,决定 block 在哪个线程中执行, nil  在发布通知的线程中执行
//usingBlock: 只要监听到通知,就会执行这个 block
//这个通知返回一个 id 这个通知同样需要移除
_observe = [[NSNotificationCenter defaultCenter] addObserverForName:@"tongzhi" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
NSLog(@"收到了通知");
}];

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


同样,这里也需要移除通知,但是这里的观察者不是 self 而是 _observe

- (void)dealloc {

//移除观察者 _observe
[[NSNotificationCenter defaultCenter] removeObserver:_observe];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    //发送通知

    dispatch_async(dispatch_get_global_queue(0, 0), ^{

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

    });

}

- (void)viewDidLoad {

    [super viewDidLoad];

    

    //多线程中使用通知

    

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

    

}

- (void)addNotification {

    NSLog(@"接受通知");

    NSLog(@"%@",[NSThread currentThread]);

    

    //主线程:监听通知,异步发送通知

    //总结: 接受通知的代码由发出通知的线程

    //更新 UI

    dispatch_sync(dispatch_get_main_queue(), ^{

        //更新 UI

    });

}

====================

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    //主线程中发送通知

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

}

- (void)viewDidLoad {

    [super viewDidLoad];

    

    //多线程中使用通知

    //异步监听通知

    

    dispatch_async(dispatch_get_global_queue(0, 0), ^{

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

    });

}

- (void)addNotification {

    NSLog(@"接受通知");

    NSLog(@"%@",[NSThread currentThread]);

    

    //异步:监听通知,主线程发送通知

    //总结: 接受通知的代码由发出通知的线程

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息