IOS代理
2016-06-15 16:18
337 查看
通知 是在跳转控制器之间常用的传值代理方式,除了代理模式,通知更方便、便捷,一个简单的Demo实现通知的跳转传值.
输入所要发送的信息 ,同时将label的值通过button方法调用传递,
- (IBAction)buttonClick:(id)sender {
//添加 字典,将label的值通过key值设置传递
NSDictionary *dict =[[NSDictionary alloc]initWithObjectsAndKeys:self.textFieldOne.text,@"textOne",self.textFieldTwo.text,@"textTwo", nil];
//创建通知
NSNotification *notification
=[NSNotification notificationWithName:@"tongzhi" object:niluserInfo:dict];
//通过通知中心发送通知
[[NSNotificationCenter defaultCenter] postNotification:notification];
[self.navigationController popViewControllerAnimated:YES];
}
在发送通知后,在所要接收的控制器中注册通知监听者,将通知发送的信息接收
- (void)viewDidLoad {
[super viewDidLoad];
//注册通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tongzhi:) name:@"tongzhi"object:nil];
}
- (void)tongzhi:(NSNotification *)text{
NSLog(@"%@",text.userInfo[@"textOne"]);
NSLog(@"-----接收到通知------");
}
移除通知:removeObserver:和removeObserver:name:object:
其中,removeObserver:是删除通知中心保存的调度表一个观察者的所有入口,而removeObserver:name:object:是删除匹配了通知中心保存的调度表中观察者的一个入口。
这个比较简单,直接调用该方法就行。例如:
[[NSNotificationCenter defaultCenter] removeObserver:observer name:nil object:self];
注意参数notificationObserver为要删除的观察者,一定不能置为nil。
输入所要发送的信息 ,同时将label的值通过button方法调用传递,
- (IBAction)buttonClick:(id)sender {
//添加 字典,将label的值通过key值设置传递
NSDictionary *dict =[[NSDictionary alloc]initWithObjectsAndKeys:self.textFieldOne.text,@"textOne",self.textFieldTwo.text,@"textTwo", nil];
//创建通知
NSNotification *notification
=[NSNotification notificationWithName:@"tongzhi" object:niluserInfo:dict];
//通过通知中心发送通知
[[NSNotificationCenter defaultCenter] postNotification:notification];
[self.navigationController popViewControllerAnimated:YES];
}
在发送通知后,在所要接收的控制器中注册通知监听者,将通知发送的信息接收
- (void)viewDidLoad {
[super viewDidLoad];
//注册通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tongzhi:) name:@"tongzhi"object:nil];
}
- (void)tongzhi:(NSNotification *)text{
NSLog(@"%@",text.userInfo[@"textOne"]);
NSLog(@"-----接收到通知------");
}
移除通知:removeObserver:和removeObserver:name:object:
其中,removeObserver:是删除通知中心保存的调度表一个观察者的所有入口,而removeObserver:name:object:是删除匹配了通知中心保存的调度表中观察者的一个入口。
这个比较简单,直接调用该方法就行。例如:
[[NSNotificationCenter defaultCenter] removeObserver:observer name:nil object:self];
注意参数notificationObserver为要删除的观察者,一定不能置为nil。
相关文章推荐
- iOS中集成ijkplayer视频直播框架
- iOS开发之duplicate symbols for architecture x86_64错误
- iOS日志获取和实时浏览器显示日志
- iOS JSON Model 转换 MJExtension
- LHProgressHUD(0.1.1)
- ios中 input 焦点光标不垂直居中
- iOS 支持 IPv6
- iOS日志获取和实时浏览器显示日志
- iOS NSHomeDirectory()根目录不能写入,没有权限访问
- 单例(GCD:dispatch_once)
- iOS10 权限崩溃问题
- 装win10 报0xc0000605
- iOS 获取图片路径的后缀/获取字符串的后缀
- iOS9 3DTouch、ShortcutItem、Peek And Pop技术一览
- iOS使用Charts框架绘制—柱形图
- iOS 获取键盘的高度
- iOS -- 基本的手势
- iOS设计模式之单列模式
- iOS开发--换肤简单实现以及工具类的抽取
- 在写一个iOS应用之前必须做的7件事(附相关资源)