IOS学习笔记30—两个ViewController间传值(一)
2016-05-19 19:03
423 查看
iOS中,在两个ViewController之间传值有多种方式,这里学习其中一种,利用临时变量在两个ViewController之间传值,这可能不是最好的传值方式,但是也是一种选择。
首先看看工程结构:
其中ViewController是第一个界面,SecondViewController是第二个界面,UserEntity为一个实体对象,用来作为在两个界面间传值的媒介。
接下来看看最后实现的效果:
在第一个界面输入完毕后,点击“传值“按钮就可以跳转到第二个界面,并显示在第一个界面中输入的值,按顶部的Back按钮则返回第一个界面。
下面来看看实现中的关键代码;
单击”传值“按钮时触发的代码:
[cpp] view
plain copy
- (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];
}
在第二个界面显示传过来值的方法:
[cpp] view
plain copy
- (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之间进行传值。
源码下载地址:源码下载
首先看看工程结构:
其中ViewController是第一个界面,SecondViewController是第二个界面,UserEntity为一个实体对象,用来作为在两个界面间传值的媒介。
接下来看看最后实现的效果:
在第一个界面输入完毕后,点击“传值“按钮就可以跳转到第二个界面,并显示在第一个界面中输入的值,按顶部的Back按钮则返回第一个界面。
下面来看看实现中的关键代码;
单击”传值“按钮时触发的代码:
[cpp] view
plain copy
- (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];
}
在第二个界面显示传过来值的方法:
[cpp] view
plain copy
- (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之间进行传值。
源码下载地址:源码下载
相关文章推荐
- IOS学习笔记31—两个ViewController间传值(二)
- iOS开发------常用正则表达式语法以及常用正则
- iOS 刘璐懒加载 进入应用程序时卡住很长时间。
- 导航栏隐藏跳转返回的坑
- iOS信号量-dispatch_semaphore
- ios开发证书CER文件、P12文件,mobileprovition许可文件的用途
- iOS 极光推送
- Xamarin.ios引用第三方SDK
- 判断iOS设备是否越狱
- iOS utf-8和urlEncode的区别
- iOS开发之代理协议
- iOS 字符串替换
- iOS 截屏+裁剪
- Ios SDWebimage Error Domain=NSURLErrorDomain Code=406 报错
- iOS IM开发建议(二)计算TableViewCell的高度:图文混排
- iOS开发常用函数
- iOS 日历方法大全
- iOS学习之——状态栏
- iOS字体打印
- iOS中邮箱、电话等正则表达验证