IOS学习笔记30—两个ViewController间传值(一)

转载自:http://blog.csdn.net/tangren03/article/details/7913754

IOS中,在两个ViewController之间传值有多种方式,这里学习其中一种,利用临时变量在两个ViewController之间传值,这可能不是最好的传值方式,但是也是一种选择。

首先看看工程结构:


其中ViewController是第一个界面,SecondViewController是第二个界面,UserEntity为一个实体对象,用来作为在两个界面间传值的媒介。

接下来看看最后实现的效果:

                            

在第一个界面输入完毕后,点击“传值“按钮就可以跳转到第二个界面,并显示在第一个界面中输入的值,按顶部的Back按钮则返回第一个界面。

下面来看看实现中的关键代码;

单击”传值“按钮时触发的代码:

[cpp]  view plain copy
  1. - (IBAction)passValueButton:(id)sender {  
  2.     //构建UserEntity对象  
  3.     UserEntity *userEntity = [[UserEntity alloc] init];  
  4.     userEntity.userName = self.userNameTextFiled.text;  
  5.     userEntity.gendar = self.gendarTextField.text;  
  6.     userEntity.age = [self.ageTextField.text intValue];  
  7.       
  8.     SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];  
  9.     //设置SecondViewController中的值  
  10.     secondView.userEntity = userEntity;  
  11.     //跳转界面  
  12.     [self presentModalViewController:secondView animated:YES];  
  13.       
  14.     [userEntity release];  
  15.     [secondView release];  
  16. }  

在第二个界面显示传过来值的方法:

[cpp]  view plain copy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.       
  5.     //显示从前一个界面传过来的值  
  6.     self.userNameTextField.text = self.userEntity.userName;  
  7.     self.gendarTextField.text = self.userEntity.gendar;  
  8.     //NSString转换为int型  
  9.     self.ageTextField.text = [NSString stringWithFormat:@"%d",self.userEntity.age];  
  10.       
  11. }  

以上就实现了一个简单的在两个ViewController之间传值的方式,下一次将使用代理模式在两个ViewController之间进行传值。

源码下载地址:源码下载

加入我们的QQ群或微信公众账号请查看: Ryan's zone公众账号及QQ群


欢迎关注我的新浪微博和我交流:@唐韧_Ryan


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值