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

ios NSNotificationCenter 的简单使用

2015-07-31 13:46 302 查看
NSNotificationCenter的作用:用于程序中不同类间的消息通信,使用步骤如下:

1,发送通知,

NSDictionary *objects = @{};//用字典存放发送通知的内容
    [[NSNotificationCenter defaultCenter] 
    postNotificationName:@"idstring" object:self userInfo:objects];


postNotificationName:通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。(postNotificationName的名字必须唯一)

2,注册通知:即要在什么地方接受消息



[[NSNotificationCenter defaultCenter] a
     ddObserver:self selector:@selector(method:)name:@"idString" object:nil];


参数介绍:

addObserver: 观察者,即在什么地方接收通知;

  selector: 收到通知后调用的方法;

  name: 通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。(通知的名字要与发送通知的名字一样,当通知发出来以后,观察者就会自动收到通知并调用相 应 的方法处理)

3,获取通知内容.


-(void) method :(NSNotification *) notification {
 NSDictionary *data= notification.userInfo;//这里取出通知内容
  //处理 ....
 }



4,移除通知

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"idString" object:nil];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: