UI中的界面之间的值传递

时间:2021-12-08 19:24:35
一. 从前往后传 —— 属性传值
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];

}