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

ios-通知简单示例

2016-04-05 00:36 435 查看
通知是一种一对多的信息广播机制,一个应用程序同时只能有一个NSNotificationCenter(通知中心)对象,用来添加通知观察者或者说监听者,以及发送通知.

用的地方是:不同控制器的传值回调.delegate和block也属于一种信息传递机制,但这两种都是一对一的,每次执行的方法都不一样,而通知是一对多,只要有地方触发通知,执行的是同一个方法。

注意点:

创建通知中心对象 添加'观察者' observer, 确定通知名字 name,确定监听对象object ,如果object 设置为 nil 则表示监听所有发送名字为 name 的消息. 以及监听到通知(收到通知后)发生的动作 @selector(动作)

让消息post 发送者发送通知 名字为在观察者哪里注册的通知名字 name,是字符串类型的.

a. 最重要的第一点是发布通知的方法中一个是最后的参数userInfo,字典类型,可以将数值传递到@selector(动作) 动作中,动作的参数为NSNotification *notification. notification.userInfo[@"key值"],里面存储了你要传递的数据.

最后要在创建观察者的控制器里面dealloc方法中移除通知中心.

下面看示例演示通知在不同控制器中的运作:



上图是两个控制器,分别绑定不同的类.

第一个控制器ViewController.m文件

//
//  ViewController.m
//  kvo01
//
//  Created by 裴波波 on 16/4/4.
//  Copyright © 2016年 裴波波. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showMessage:) name:@"show" object:nil];
}


在viewDidLoad添加观察者,以及观察者收到通知后触发的方法.设置消息名字.监听对象nil(所有名字为"show"的通知)

//控制器发送名字为 @"show" 的通知的时候都会执行该方法
-(void)showMessage:(NSNotification *)notification {

//弹框
UIAlertController * alertVc = [UIAlertController alertControllerWithTitle:@"操作提示" message:@"这是通知中心" preferredStyle:UIAlertControllerStyleAlert];
//利用userInfo是字典,将需要的数据传递进来,一个是字符串
NSString * str = notification.userInfo[@"haha"];
UIAlertAction * act = [UIAlertAction actionWithTitle:str style:UIAlertActionStyleDefault handler:nil];
[alertVc addAction:act];
//另一个是将触发这个方法的控制器(self)传递进来--->谁发送的 @"show" 通知 触发的这个方法--弹框,就由哪个Vc来弹出这个弹框,而不是用self presentViewController ---这样不对.
[notification.userInfo[@"vc"] presentViewController:alertVc animated:YES completion:nil];
}


由于上述方法是弹框的方法所以应该是谁调用这个方法,在谁的控制器里面弹框.所以利用了下面的方法(PBBViewController里面利用userInfo字典将PBBViewcontroller本身传递进来,让self presentViewController 转换成传递进来的PBBViewcontroller).

//移除通知一般在dealloc中
-(void)dealloc{

[[NSNotificationCenter defaultCenter] removeObserver:[ViewController class] name:@"show" object:nil];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end


第二个控制的.m文件

//
//  PBBViewController.m
//  kvo01
//
//  Created by 裴波波 on 16/4/4.
//  Copyright © 2016年 裴波波. All rights reserved.
//

#import "PBBViewController.h"

@interface PBBViewController ()

@end

@implementation PBBViewController
- (IBAction)clickSendMessageButton:(id)sender {

//获得通知中心对象,发送通知,通知名为在ViewController中注册的通知名 @"show" ,ViewController为观察者,接收通知.接收到通知后出发对应的 @selector(方法) 方法.由于出发的 '方法' 是弹框操作,需要 present 一个controller,而且是谁发送的这个通知,在谁的controller中弹出alertViewController.---通过userInfo字典类型,将对象包装在内部,回传给通知观察者.将调用 present... controller的对象改为传递过去的控制器对象  下面的 @"vc" : (对应的) self ,还可以将弹框的message 改为 @"这是哈哈"
[[NSNotificationCenter defaultCenter] postNotificationName:@"show" object:nil userInfo:@{@"vc":self,@"haha":@"这是哈哈"}];
}

- (void)viewDidLoad {

[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end


注意点:postNotificationName此方法中将,需要的控制器传递给ViewController了,以及弹框的message也改成了传递进去的 @"这是哈哈"


能用kvo 以及 block 回调 来传递信息,就不用delegate来传递信息.代理比较繁琐.

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