IOS学习笔记30—两个ViewController间传值(一)
来源:互联网 发布:高考辅导软件 编辑:程序博客网 时间:2024/04/30 04:07
IOS中,在两个ViewController之间传值有多种方式,这里学习其中一种,利用临时变量在两个ViewController之间传值,这可能不是最好的传值方式,但是也是一种选择。
首先看看工程结构:
其中ViewController是第一个界面,SecondViewController是第二个界面,UserEntity为一个实体对象,用来作为在两个界面间传值的媒介。
接下来看看最后实现的效果:
在第一个界面输入完毕后,点击“传值“按钮就可以跳转到第二个界面,并显示在第一个界面中输入的值,按顶部的Back按钮则返回第一个界面。
下面来看看实现中的关键代码;
单击”传值“按钮时触发的代码:
- - (IBAction)passValueButton:(id)sender {
- //构建UserEntity对象
- UserEntity *userEntity = [[UserEntity alloc] init];
- userEntity.userName = self.userNameTextFiled.text;
- userEntity.gendar = self.gendarTextField.text;
- userEntity.age = [self.ageTextField.text intValue];
- SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
- //设置SecondViewController中的值
- secondView.userEntity = userEntity;
- //跳转界面
- [self presentModalViewController:secondView animated:YES];
- [userEntity release];
- [secondView release];
- }
在第二个界面显示传过来值的方法:
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //显示从前一个界面传过来的值
- self.userNameTextField.text = self.userEntity.userName;
- self.gendarTextField.text = self.userEntity.gendar;
- //NSString转换为int型
- self.ageTextField.text = [NSString stringWithFormat:@"%d",self.userEntity.age];
- }
以上就实现了一个简单的在两个ViewController之间传值的方式,下一次将使用代理模式在两个ViewController之间进行传值。
源码下载地址:源码下载
加入我们的QQ群或微信公众账号请查看:Ryan's zone公众账号及QQ群
欢迎关注我的新浪微博和我交流:@唐韧_Ryan
转自:http://blog.csdn.net/ryantang03/article/details/7913754
0 0
- IOS学习笔记30—两个ViewController间传值(一)
- IOS学习笔记30—两个ViewController间传值(一)
- IOS学习笔记30—两个ViewController间传值(一)
- IOS学习笔记30—两个ViewController间传值(一)
- IOS学习笔记30—两个ViewController间传值(一)
- IOS学习笔记30—两个ViewController间传值(一)
- IOS学习笔记30—两个ViewController间传值(一)
- IOS学习笔记31—两个ViewController间传值(二)
- IOS学习笔记31—两个ViewController间传值(二)
- IOS学习笔记31—两个ViewController间传值(二)
- IOS学习笔记31—两个ViewController间传值(二)
- IOS学习笔记31—两个ViewController间传值(二)
- IOS学习笔记31—两个ViewController间传值(二)
- IOS学习笔记31—两个ViewController间传值(二)
- ios 两个ViewController间传值(一)
- IOS—两个ViewController间传值
- 两个ViewController间传值(一)
- 两个ViewController间传值(一)
- 生成config.h
- 搭建自己的OpenWrt开发环境
- nagios插件之监控web页面登陆
- ICE同步调用与异步调用(3)
- cbench安装+总结
- IOS学习笔记30—两个ViewController间传值(一)
- 7月自动化测试公开课通知
- MySQL字段类型及长度值
- asp中querystring的用法
- 重写org.springframework.beans.BeanUtils的copyProperties(Object source,Object target)方法 从model复制属性到pojo中
- SQLITE3的锁以及事务
- IOS学习笔记31—两个ViewController间传值(二)
- struts2 自定义全局类型转换器
- HDU 1305 Immediate Decodability