UI中的界面之间的值传递 <一>
2015-08-27 20:24
441 查看
一. 从前往后传 —— 属性传值
1< 在后面一个界面定义属性(属性的类型要和传输的数据的数据类型一致).
2< 在从前一个界面进入到下一个界面之前, 将数据传给下一个界面.
例如有两个视图控制器:
FirstViewController 和 SecondViewController
在这两个视图控制器中设置一个textField 和 label, 并且把FirstViewController的view上的textField上输入的text显示到SecondViewController的label上.
在FirstViewController.m文件中代码如下:
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 进入下一界面的按钮
[self setupButton];
[self setupTextField];
[self setupLabel];
}
// 创建button
- (void)setupButton {
UIButton *pushBtn = [UIButton buttonWithType:UIButtonTypeSystem];
pushBtn.frame = CGRectMake(20, 100, 280, 40);
[pushBtn setTitle:@"进入下一个界面" forState:UIControlStateNormal];
pushBtn.backgroundColor = [UIColor blueColor];
[pushBtn addTarget:self action:@selector(handlePushBtn:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pushBtn];
}
// 创建textField
- (void)setupTextField {
UITextField *textFiled = [[UITextField alloc] initWithFrame:CGRectMake(20, 230, 280, 40)];
textFiled.placeholder = @"显示到第二个界面上";
[self.view addSubview:textFiled];
textFiled.tag = 100;
textFiled.backgroundColor = [UIColor grayColor];
[textFiled release];
}
// 创建label
- (void)setupLabel {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 300, 280, 40)];
label.backgroundColor = [UIColor whiteColor];
label.layer.borderWidth = 2;
label.layer.cornerRadius = 5;
label.tag = 200;
[self.view addSubview:label];
[label release];
}
#pragma mark -- button action
- (void)handlePushBtn:(UIButton *)sender {
// 1. 创建的二个视图控制器
SecondViewController *secondVC = [[SecondViewController alloc] init];
// 第二个显示第一个textField.text
secondVC.data = [(UITextField *)[self.view viewWithTag:100] text];
// 2. 通过导航控制push到下一个界面(视图控制器自带的navigationController 属性能够获取到管理当前视图控制器的导航控制器, 然后, 再通过导航控制器进行push)
[self.navigationController pushViewController:secondVC animated:YES]; // 该行代码表示进入下一个界面.
// 3. 释放
[secondVC release];
@end
在SecondViewController.h文件中代码如下:
#import <UIKit/UIKit.h>
@protocol SecondViewControllerDelegate <NSObject>
- (void)passValue:(NSString *)data;
@end
@interface SecondViewController : UIViewController
// 属性传值第一步: 后一个界面定义属性
@property (nonatomic, copy) NSString *data;
@end
在SecondViewController.m 文件中
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// 设置上一界面的按钮
[self setupPopButton];
[self setupTextField];
[self setupLabel];
}
- (void)setupTextField {
UITextField *textFiled = [[UITextField alloc] initWithFrame:CGRectMake(20, 230, 280, 40)];
textFiled.placeholder = @"显示到上一个界面上";
[self.view addSubview:textFiled];
textFiled.tag = 200;
textFiled.backgroundColor = [UIColor grayColor];
[textFiled release];
}
- (void)setupLabel {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 300, 280, 40)];
label.backgroundColor = [UIColor whiteColor];
label.layer.borderWidth = 2;
label.layer.cornerRadius = 5;
label.text = self.data;
[self.view addSubview:label];
[label release];
}
- (void)setupPopButton {
UIButton *popBtn = [UIButton buttonWithType:UIButtonTypeSystem];
popBtn.frame = CGRectMake(20, 150, 280, 40);
[popBtn setTitle:@"进入上一个界面" forState:UIControlStateNormal];
popBtn.backgroundColor = [UIColor blueColor];
[popBtn addTarget:self action:@selector(handlePopBtn:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:popBtn];
}
- (void)handlePopBtn:(UIButton *)sender {
// 1. 返回上一界面
[self.navigationController popViewControllerAnimated:YES];
}
- (void)dealloc {
[_data release];
[super dealloc];
}
1< 在后面一个界面定义属性(属性的类型要和传输的数据的数据类型一致).
2< 在从前一个界面进入到下一个界面之前, 将数据传给下一个界面.
例如有两个视图控制器:
FirstViewController 和 SecondViewController
在这两个视图控制器中设置一个textField 和 label, 并且把FirstViewController的view上的textField上输入的text显示到SecondViewController的label上.
在FirstViewController.m文件中代码如下:
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 进入下一界面的按钮
[self setupButton];
[self setupTextField];
[self setupLabel];
}
// 创建button
- (void)setupButton {
UIButton *pushBtn = [UIButton buttonWithType:UIButtonTypeSystem];
pushBtn.frame = CGRectMake(20, 100, 280, 40);
[pushBtn setTitle:@"进入下一个界面" forState:UIControlStateNormal];
pushBtn.backgroundColor = [UIColor blueColor];
[pushBtn addTarget:self action:@selector(handlePushBtn:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pushBtn];
}
// 创建textField
- (void)setupTextField {
UITextField *textFiled = [[UITextField alloc] initWithFrame:CGRectMake(20, 230, 280, 40)];
textFiled.placeholder = @"显示到第二个界面上";
[self.view addSubview:textFiled];
textFiled.tag = 100;
textFiled.backgroundColor = [UIColor grayColor];
[textFiled release];
}
// 创建label
- (void)setupLabel {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 300, 280, 40)];
label.backgroundColor = [UIColor whiteColor];
label.layer.borderWidth = 2;
label.layer.cornerRadius = 5;
label.tag = 200;
[self.view addSubview:label];
[label release];
}
#pragma mark -- button action
- (void)handlePushBtn:(UIButton *)sender {
// 1. 创建的二个视图控制器
SecondViewController *secondVC = [[SecondViewController alloc] init];
// 第二个显示第一个textField.text
secondVC.data = [(UITextField *)[self.view viewWithTag:100] text];
// 2. 通过导航控制push到下一个界面(视图控制器自带的navigationController 属性能够获取到管理当前视图控制器的导航控制器, 然后, 再通过导航控制器进行push)
[self.navigationController pushViewController:secondVC animated:YES]; // 该行代码表示进入下一个界面.
// 3. 释放
[secondVC release];
@end
在SecondViewController.h文件中代码如下:
#import <UIKit/UIKit.h>
@protocol SecondViewControllerDelegate <NSObject>
- (void)passValue:(NSString *)data;
@end
@interface SecondViewController : UIViewController
// 属性传值第一步: 后一个界面定义属性
@property (nonatomic, copy) NSString *data;
@end
在SecondViewController.m 文件中
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// 设置上一界面的按钮
[self setupPopButton];
[self setupTextField];
[self setupLabel];
}
- (void)setupTextField {
UITextField *textFiled = [[UITextField alloc] initWithFrame:CGRectMake(20, 230, 280, 40)];
textFiled.placeholder = @"显示到上一个界面上";
[self.view addSubview:textFiled];
textFiled.tag = 200;
textFiled.backgroundColor = [UIColor grayColor];
[textFiled release];
}
- (void)setupLabel {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 300, 280, 40)];
label.backgroundColor = [UIColor whiteColor];
label.layer.borderWidth = 2;
label.layer.cornerRadius = 5;
label.text = self.data;
[self.view addSubview:label];
[label release];
}
- (void)setupPopButton {
UIButton *popBtn = [UIButton buttonWithType:UIButtonTypeSystem];
popBtn.frame = CGRectMake(20, 150, 280, 40);
[popBtn setTitle:@"进入上一个界面" forState:UIControlStateNormal];
popBtn.backgroundColor = [UIColor blueColor];
[popBtn addTarget:self action:@selector(handlePopBtn:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:popBtn];
}
- (void)handlePopBtn:(UIButton *)sender {
// 1. 返回上一界面
[self.navigationController popViewControllerAnimated:YES];
}
- (void)dealloc {
[_data release];
[super dealloc];
}
相关文章推荐
- UIView的各种方法介绍和生命周期
- SpannableStringBuilder 和 SpannableString
- iOS:UIPickerView选择器的使用
- ligerUI form表单里的filed的类型根据另外个filed的值改变而改变
- UVA 10735 Euler Circuit (最大流)
- HDU 1005 Number Sequence(矩阵快速幂)
- 无需看到你的脸就能认出你——实现Beyond Frontal Faces: Improving Person Recognition Using Multiple Cues
- Android UI设计:AlertDialog
- iOS-UIViewController的UIView生命周期
- iOS:步进UIStepper、滑动块UISlider、开关UISwitch的基本使用
- UITaleViewCell
- uitextfield 当设置为selectedAll 时不让 复制、粘帖那几项自动出来的解决办法
- Building a Space Station(POJ--2031【最小生成树】
- UITableView的编辑
- 08-25 convertView、ViewHolder、MyfruitPractice、divider、Header
- JSF 2 hidden value example
- UIImageView添加圆角图片
- Ngui如何弹出提示消息,并淡出【支持多条】
- 8.8.1 Optimizing Queries with EXPLAIN
- amaze UI animation动画示例