美文网首页
09-通知(NSNotification)

09-通知(NSNotification)

作者: 小胖子2号 | 来源:发表于2017-03-07 09:28 被阅读18次

1、NSNotification

这个类可以理解为一个消息对象,其中有三个成员变量。

  • 这个成员变量是这个消息对象的唯一标识,用于辨别消息对象。
    @property (readonly, copy) NSString *name;

  • 这个成员变量定义一个对象,可以理解为针对某一个对象的消息。
    @property (readonly, retain) id object;

  • 这个成员变量是一个字典,可以用其来进行传值。
    @property (readonly, copy) NSDictionary *userInfo;

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、如果发送的通知指定了object对象,那么观察者接收的通知设置的object对象与其一样,才会接收到通知,但是接收通知如果将这个参数设置为了nil,则会接收一切通知。

2、观察者的SEL函数指针可以有一个参数,参数就是发送的死奥西对象本身,可以通过这个参数取到消息对象的userInfo,实现传值。

通知使用流程

  • 1、在需要发送通知的地方,发送通知
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center postNotificationName:MSA_LOGIN_NOTIFICATION object:nil userInfo:@{@"errorCode" : @0 , @"ticket" : model.authTicket}];
  • 2、接受通知
 NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
 [center addObserver:self selector:@selector(verifySSOTicket:) name:MSA_LOGIN_NOTIFICATION object:nil];

// 登录结果(接收通知后要做的事情)
- (void)verifySSOTicket:(NSNotification *)notic
{
    NSLog(@"notic:%@", notic.userInfo);
    ViewController *vc = [[ViewController alloc] init];
    self.window.rootViewController = vc;
}

相关文章

  • 09-通知(NSNotification)

    1、NSNotification 这个类可以理解为一个消息对象,其中有三个成员变量。 这个成员变量是这个消息对象的...

  • NSNotification

    发通知 NSNotification *deleteMyCommemtN =[NSNotification not...

  • iOS 中通知机制NSNotification

    iOS 中通知机制详解 NSNotification 通知的对象,一条通知就是一个NSNotification对象...

  • 推送通知-本地推送

    iOS推送通知 注意:这里说的推送通知跟NSNotification有所区别 NSNotification是抽象的...

  • iOS 推送后台语音播报

    推送通知 注意:这里说的推送通知跟NSNotification有所区别 NSNotification是抽象的,不可...

  • iOS本地推送

    1. 推送通知简介 1.1: 这里说的推送通知跟NSNotification有所区别 NSNotification...

  • NSNotification 通知

    +++Categories = ["iOS",]Tags = ["iOS","NSNotification",]d...

  • 通知——NSNotification

    转自http://blog.sina.com.cn/s/blog_6317728d0102v779.html不会格...

  • 通知NSNotification

    通知中心 通知中心-- 每一个应用程序中都有一个NSNotificationCenter的实例(对象),它是负责帮...

  • 通知NSNotification

    通知基本使用,如图 需要注意的有两点: 1.使用通知,一定要先创建监听者,再发送通知,不然是接收不到通知的。 2....

网友评论

      本文标题:09-通知(NSNotification)

      本文链接:https://www.haomeiwen.com/subject/qyjgmttx.html