NSNotification的使用

NSNotification的使用

一、NSNotification的使用

1、第一步 通知发出,比如在视图控制A

NSDictionary *dict = @{@"headImageURL":@"http://www.mmbao.com/x.jpg",

                       @"buyerUserName":@"mmbqwe",

                       @"buyerNickName":@"冒菜"

};

NSNotification *notification = [NSNotification notificationWithName: @"notificationAction"  object:nil userInfo:dict];

[[NSNotificationCenter defaultCenter] postNotification:notification];

2、第二步 接受通知,即将通知添加到本视图中,比如视图B。

(1) [[NSNotificationCenter defaultCenter] addObserver: self selector:@selector(notificationAction:) name: @"notificationAction" object:nil];  //注册通知监听器

(2)实现响应的方法

- (void)notificationAction:(NSNotification *)notification

{

     NSLog(@"%@%@", notification.userInfo, notification.name);

}

(3)在本视图析构的时候,将通知移除

- (void)dealloc

{

      [[NSNotificationCenter defaultCenter] removeObserver:self name: @"notificationAction" object:nil];

}

最终输出:

 EVNTextFieldTest[5506:428707] {

buyerNickName = "\U5192\U83dc";

buyerUserName = mmbqwe;

headImageURL = "http://www.mmbao.com/xxxxxxx.jpg";

}notificationAction

二、属性介绍

1.通知的属性

@property (readonly, copy) NSString *name; // 通知的名称

@property (readonly, retain) id object;  // 通知的发布者

@property (readonly, copy) NSDictionary *userInfo; // 一些额外的信息

2.初始化一个通知对象

- (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;

3.发布通知

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

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

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

4.注册通知监听器

(1)

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

//  observer:监听器

//  aSelector:收到通知后回到的方法,把通知对象当做参数传入

//  aName:通知的名字,为nil,无论通知名称是什么,监听器都能接受到这个通知

//  anObject:通知的发布者,anObject和name 都为nil,监听器能收到所有的通知

(2)

- (id)addObserverForName:(NSString *)name object:(id)obj queue:(NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block;

//  name:通知的名称

//  obj:通知的发布者

//  block:收到对应的通知时,会回到的这个block

//  queue:决定block在那个队列中执行

5.注销通知监听器

- (void)removeObserver:(id)observer;

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

通知中心不会保留监听器对象,在通知中心注册过的对象,必须在该对象释放前取消注册,否则相应的通知再次出现时,通知中心仍然会向监听器对象发送通知,相应的监听器已经不存在了,可能会导致应用崩溃

一般的用法:

// 监听通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changetext) name:UITextFieldTextDidChangedNotification object:self.accoutField];

// 接受到通知后执行的操作

- (void)changetext{}

// 移除监听

- (void)dealloc

{

    [[NSNotificationCenter defaultCenter] removeObserver:self]; // 移除当前文件中所有的通知

     //  [[NSNotificationCenter defaultCenter] removeObserver:self name:@"notificationAction" object:nil]; // 移除当前文件中指定名字的通知

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值