iPhone消息通知

1.NSNotification:消息或通知

有三个成员变量

- (NSString *)name;

- (id)object;

- (NSDictionary *)userInfo;

通知名称:name,

消息发送者:object,代理在收到NSNotification方法里,可以回调到object

附加信息:userInfo

 

2.NSNotificationCenter:消息中心

单例模式,需要通过以下类方法访问

[NSNotificationCenter defaultCenter]

3.广播一个通知

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

发送一个附带信息的通知

[[NSNotificationCenter defaultCenter] postNotificationName:@"aEvent" object:nil userInfo:(NSDictionary *)aUserInfo];
aUserInfo是一个字典

直接发送一个通知

[[NSNotificationCenter defaultCenter] postNotification:(NSNotification *)notification]];

4.注册一个通知监听

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

注册通知时,可以指定一个具体的广播者对象object,但不是必须的

5.移除一个监听

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

6.通知要调用的方法

- (void)onHandler:(NSNotification *)notif
{
NSDictionary *user_info = [notif userInfo];
  //执行自定逻辑
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值