使用通知机制,在dismissViewControllerAnimated后,completion传值给上一个父视图方法
2015-10-13 09:01
621 查看
dismissViewControllerAnimated后,completion传值给上一个父视图方法
视图firstView和secendView,点击firstView上面的按钮presentviewcontroller出secendView;secendView上有个按钮,点击按钮dismissViewControllerAnimated,并将某个值传给firstView,或不直接在firstView里面的viewWillAppear里面调用方法,而是直接通过在dismissViewControllerAnimated
completion里面编辑代码块调用firstView里面的任何方法,该怎么做?
这个问题其实并不复杂,如果你知道如何使用NSNotificationCenter实现起来还是非常简单的。
先说一下,secendView在dismissViewControllerAnimated后,如何在进入firstView后,自动调用firstView里面的任何方法
第一步:在secendView里面,点击按钮时调用一个方法,该方法为:
-(void)secendAction{
[self dismissViewControllerAnimated:YES completion:^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"do" object:self];
}];
}
上面代码是将secendView dismissViewControllerAnimated掉,然后自动注册一个名为do的通知
注册了这个名为的通知,你就可以在任何.m文件里面通过以下代码调用到了:
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(handleColorChange:)
name:@"do"
object:nil];
上面的代码的意思就是,先找到已经注册过的名为do的通知,然后再自动调用handleColorChange去处理,
所以:
第二步:在firstView里面的viewWillAppear方法里面写入以下代码:
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(handleColorChange:)
name:@"do"
object:nil];
handleColorChange方法为:
-(void)handleColorChange:(id)sender{
[self
firstView里面方法]
}
看明白了吧?在secendView里面我们不直接调用firstView里面的方法,而是通过通知来让firstView自动调用自己里面的某个方法。
通过通知可以让不同.m文件之间进行方法和参数的传递
ok就下来说一下如何在dismissViewControllerAnimated后将secendView里面的值传递给firstView
第一步:在secendView里面,点击按钮时调用一个方法,该方法为:
-(void)secendAction{
[self dismissViewControllerAnimated:YES completion:^{
[tools showToast:@"图片信息提交成功" withTime:1500 withPosition:iToastGravityCenter];
[[NSNotificationCenter defaultCenter] postNotificationName:@"do" object:self userInfo:dictionary];
}];
}
userInfo:dictionary里面的dictionary就是你要传递的字典对象的值
第二步:在firstView里面的viewWillAppear方法里面写入以下代码:
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(handleColorChange:)
name:@"do"
object:nil];
handleColorChange方法为:
-(void)handleColorChange:(NSNotification*)sender{
NSLog(@"%@",sender);
[self
firstView里面方法]
}
-(void)handleColorChange:(NSNotification*)sender里面的sender就是你在secendView里面所传递的字典对象的值,简单吧?!
视图firstView和secendView,点击firstView上面的按钮presentviewcontroller出secendView;secendView上有个按钮,点击按钮dismissViewControllerAnimated,并将某个值传给firstView,或不直接在firstView里面的viewWillAppear里面调用方法,而是直接通过在dismissViewControllerAnimated
completion里面编辑代码块调用firstView里面的任何方法,该怎么做?
这个问题其实并不复杂,如果你知道如何使用NSNotificationCenter实现起来还是非常简单的。
先说一下,secendView在dismissViewControllerAnimated后,如何在进入firstView后,自动调用firstView里面的任何方法
第一步:在secendView里面,点击按钮时调用一个方法,该方法为:
-(void)secendAction{
[self dismissViewControllerAnimated:YES completion:^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"do" object:self];
}];
}
上面代码是将secendView dismissViewControllerAnimated掉,然后自动注册一个名为do的通知
注册了这个名为的通知,你就可以在任何.m文件里面通过以下代码调用到了:
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(handleColorChange:)
name:@"do"
object:nil];
上面的代码的意思就是,先找到已经注册过的名为do的通知,然后再自动调用handleColorChange去处理,
所以:
第二步:在firstView里面的viewWillAppear方法里面写入以下代码:
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(handleColorChange:)
name:@"do"
object:nil];
handleColorChange方法为:
-(void)handleColorChange:(id)sender{
[self
firstView里面方法]
}
看明白了吧?在secendView里面我们不直接调用firstView里面的方法,而是通过通知来让firstView自动调用自己里面的某个方法。
通过通知可以让不同.m文件之间进行方法和参数的传递
ok就下来说一下如何在dismissViewControllerAnimated后将secendView里面的值传递给firstView
第一步:在secendView里面,点击按钮时调用一个方法,该方法为:
-(void)secendAction{
[self dismissViewControllerAnimated:YES completion:^{
[tools showToast:@"图片信息提交成功" withTime:1500 withPosition:iToastGravityCenter];
[[NSNotificationCenter defaultCenter] postNotificationName:@"do" object:self userInfo:dictionary];
}];
}
userInfo:dictionary里面的dictionary就是你要传递的字典对象的值
第二步:在firstView里面的viewWillAppear方法里面写入以下代码:
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(handleColorChange:)
name:@"do"
object:nil];
handleColorChange方法为:
-(void)handleColorChange:(NSNotification*)sender{
NSLog(@"%@",sender);
[self
firstView里面方法]
}
-(void)handleColorChange:(NSNotification*)sender里面的sender就是你在secendView里面所传递的字典对象的值,简单吧?!
相关文章推荐
- OpenStack云王者归来---第一章OpenStack概述
- 设计模式之命令模式
- ARC, MRC 的混编
- 各大门户网站的css初始化代码
- 浅谈Nodejs观察者模式
- 那些在学习iOS开发前就应该知道的事(part 1)
- 省市区无刷新联动
- 从3D Touch 看 原生快速开发
- POJ 1084 搜索(IDA*)
- 互联网创业必备工具盘点
- “深度学习”学习笔记之深度信念网络 Deep Belief Network(DB)
- 相机拍的图,电脑上画的图,word里的文字,电脑屏幕,手机屏幕,相机屏幕显示大小一切的一切都搞明白了!
- POJ 1703:Find them, Catch them
- Zabbix 自定义Item经常Not Supported解决,页面出现不支持
- sicily1031Campus之最短路径解题报告
- POJ3321 Apple Tree(DFS序)
- MySQL常用操作
- 朴素UNIX之-打开历史
- python tab键自动补齐命令
- CString和string的互相转换