iOS Storyboard 中多个view 之间传送数据

iOS Storyboard 中多个view 之间传送数据

在iOS中,多个View 之间交换数据是最常见的。比如点击联络人列表中某个人,然后新窗口显示这个联络人详情。

如果这两个view 是由代码完成生成,通过操作对象指针可以简单处理。但是如果两个view是在Storyboard绘制如何处理?而且跳转关系是自动调用perfomSegua调转,这时已经无法看对象指针。

一. 使用要点

首先参见两篇教程

参见 http://www.mybringback.com/tutorial-series/12396/ios-passing-data-between-views/

http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/

+在跳转前的view 重载prepareForSegue

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

  • segua中有一个变量destinationViewController即是下一个view的实例
    对其赋值即可

二.代码片断

示例1:

-(void)prepareForSegue:(UIStoryboardSegue )segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
NSIndexPath
indexPath = [self.tableView indexPathForSelectedRow];
RecipeDetailViewController *destViewController = segue.destinationViewController;
destViewController.recipeName = [recipes objectAtIndex:indexPath.row];
}
}

注意这里有一个强制转换
RecipeDetailViewController *destViewController =segue.destinationViewController;

示例2:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([segue.identifier isEqualToString:@"hello"]) {

NSString *intro = @"Hello my friend.";

ViewController2 *vc = [segue destinationViewController];

vc.introString = intro;

} else if ([segue.identifier isEqualToString:@"hola"]) {

NSString *intro = @"Hola mi amigo.";

ViewController2 *vc = [segue destinationViewController];

vc.introString = intro;

} else if ([segue.identifier isEqualToString:@"bam"]) {

UIImage *image = [UIImage imageNamed:@"12.png"];

ViewController2 *vc = [segue destinationViewController];

vc.introImage = image;

}

}

测试程序下载
http://dchabmztumu0g.cloudfront.net/wp-content/uploads/2012/10/prepareForSegueTut2.zip

https://dl.dropboxusercontent.com/u/2857188/RecipeBookSimple.zip

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值