页面跳转与数据传递
2015-07-18 18:31
309 查看
将数据从 ViewControllerA 传递到 ViewControllerB
1、建立数据传递协议
2、ViewControllerA 中的代码
首先,声明委托,记录 ViewControllerB
// 数据传递的目标视图
其次,在页面跳转的命令中使用Segue进行页面跳转
最后,重载下面的接口,在页面跳转之前准备需要传递的数据
下面的方法可以决定是否允许Segue跳转
3、ViewControllerB 中的代码
为控制器 ViewControllerB 添加 <PassValueDelegate> 协议,实现该协议
4、封装需要传递的数据
声明自定义数据到字典的转换
在自定义数据结构中实现该协议
1、建立数据传递协议
// 数据传递协议 @protocol PassValueDelegate <NSObject> -(void)setData:(NSMutableDictionary *)dictionary; @end
2、ViewControllerA 中的代码
首先,声明委托,记录 ViewControllerB
// 数据传递的目标视图
@property(nonatomic,assign)NSObject<PassValueDelegate> *delegate;
其次,在页面跳转的命令中使用Segue进行页面跳转
[self performSegueWithIdentifier:@"RegionID" sender:nil];
最后,重载下面的接口,在页面跳转之前准备需要传递的数据
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ if([segue.identifier isEqualToString:@"DoSomething"]){ self.delegate = segue.destinationViewController; // 更新UI到数据,获取数据字典 [self updateDataFromUI]; NSMutableDictionary *dictionary = [self.currentRegionData transfromToDictionary]; // 调用代理,传递数据 [self.delegate setData:dictionary]; } }
下面的方法可以决定是否允许Segue跳转
-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender{ if ([identifier isEqualToString:@"mySegueID"]) { return [self canSegue] ? YES : NO; } }
3、ViewControllerB 中的代码
为控制器 ViewControllerB 添加 <PassValueDelegate> 协议,实现该协议
-(void)setData:(NSMutableDictionary *)dictionary { RegionData *data = [[RegionData alloc] init]; [data FillWithDictionary:dictionary]; }
4、封装需要传递的数据
声明自定义数据到字典的转换
// 转换数据到字典 @protocol TransfromToDictionary <NSObject> -(NSMutableDictionary *)transfromToDictionary; -(void)FillWithDictionary:(NSMutableDictionary *) dictionary; @end
在自定义数据结构中实现该协议
-(NSMutableDictionary *)transfromToDictionary{ NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity: 1]; [dictionary setValue:self forKey:@"regionData"]; return dictionary; } -(void)FillWithDictionary:(NSMutableDictionary *) dictionary{ RegionData *data = [dictionary objectForKey:@"regionData"]; self.regionName = data.regionName; self.regionDetail = data.regionDetail; self.regionImage = data.regionImage; self.location = data.location; }
相关文章推荐
- 快速排序-java
- git常用命令整理
- 嵌入式Linux利用Wifi搭建无线服务器(物联网实践之无线网关)
- 计算机网络(一)
- 今天搞了一天的CentOS,唉,实在感觉自己渺小啊
- 贪心 Codeforces Round #191 (Div. 2) A. Flipping Game
- springmvc的3中路径风格
- c++题目整理
- Java JVM 垃圾回收
- 3个相关联的的HTML页面的简单设计
- 关于二维数组和指针与传参
- 二分类模型性能评价 2.0(ROC曲线,lift曲线,lorenz曲线)
- windows版的weblogic11g设置成服务开机启动
- 通过XML PATH在select中进行字符串合并(sql2005)
- DP Codeforces Round #FF (Div. 1) A. DZY Loves Sequences
- Asp.Net 4.5网站使用IIS发布 uploadify插件 上传文件大小问题
- 两种方式将Eclipse中的项目生成git并且提交到github上面
- 前端属性,写法等综合随记(老是搞忘!)
- poj 3088 Push Botton Lock(斯特林数+组合数学)
- Linux-6.5-服务器模型