界面传值(+导航控制器) 复习

2016-02-24  本文已影响46人  雷仔

前提是想要实现界面传值 必须建立两个页面 这样才能实现界面之间的传值

在第一个页面RootViewController中

首先要引入第二个页面的头文件 SecondViewController.h

!!!!!!!!!!!!!在RootViewController.h 中写的

#import "RootViewController.h"

#import "TwoViewController.h"@interface RootViewController ()

//如果想要是方法中也能使用在属性中的TextField 必须建立一个属性

@property (nonatomic, retain)UITextField *tf1;

@end

@implementation RootViewController

//建立属性 必须dealloc

- (void)dealloc

{

[_tf1 release];

[super dealloc];

}

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColor lightGrayColor];

//以下属于设置navigationController的属性

//改变navigationController控制器栏的颜色

self.navigationController.navigationBar.barTintColor = [UIColor grayColor];

self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

self.navigationController.navigationItem.title = @"猴赛雷";

//设置SegmentedControl的属性

UISegmentedControl *seg = [[[UISegmentedControl alloc]initWithItems:@[@"消息",@"电话"]]autorelease];

seg.frame = CGRectMake(0, 0, 100, 40);

self.navigationItem.titleView = seg;

//在左上角建立一个按钮

self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(leftclick)]autorelease];

//通过属性建立的TextField 设置TextField

self.tf1 = [[UITextField alloc]initWithFrame:CGRectMake(50, 164, 150, 50)];

self.tf1.backgroundColor = [UIColor whiteColor];

[self.view addSubview:self.tf1];

[self.tf1 release];

self.tf1.placeholder = @"请输入......";

self.tf1.secureTextEntry = YES;

self.tf1.clearButtonMode = YES;

self.tf1.layer.borderColor = [UIColor cyanColor].CGColor;

self.tf1.layer.borderWidth = 2;

self.tf1.layer.cornerRadius = 2;

//设置button的属性

UIButton *bt1 = [UIButton buttonWithType:UIButtonTypeSystem];

bt1.frame = CGRectMake(0, 0, 50, 30);

bt1.center = CGPointMake(self.view.center.x, self.view.frame.size.height - 50);

bt1.backgroundColor = [UIColor yellowColor];

[self.view addSubview:bt1];

[bt1 addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];

[bt1 setTitle:@"点击" forState:UIControlStateNormal];

}

//实现左按钮的方法

- (void)leftclick

{

}

//实现button的方法 在方法内想要使页面进行传值的效果

- (void) click

{

//通过页面2建立一个对象 通过代理使页面1的text值传递给页面1

TwoViewController *two = [[TwoViewController alloc]init];

two.str = self.tf1.text;

two.delegate = self;

//push 通过push的方法传递给页面2

[self.navigationController pushViewController:two animated:YES];

}

//这是最后实现的代理方法

- (void)passValue:(NSString *)string

{

self.tf1.text = string;

}

**********************************************************

!!!!!!!!!!!!!!在页面2中SecondViewController.m中写

#import "TwoViewController.h"

@interface TwoViewController ()

//咋页面2中也得写属性 要不TextField无法再方法中使用

@property (nonatomic, retain)UITextField *tf2;

@end

@implementation TwoViewController

- (void)dealloc

{

[_str release];

[_tf2 release];

[super dealloc];

}

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColor redColor];

//建立一个页面2的TextField

self.tf2 = [[UITextField alloc]initWithFrame:CGRectMake(50, 164, 150, 50)];

self.tf2.backgroundColor = [UIColor whiteColor];

[self.view addSubview:self.tf2];

[self.tf2 release];

self.tf2.placeholder = @"请输入......";

self.tf2.clearButtonMode = YES;

self.tf2.layer.borderColor = [UIColor cyanColor].CGColor;

self.tf2.layer.borderWidth = 2;

self.tf2.layer.cornerRadius = 2;

//建立一个按钮 是为了点击返回页面1的

UIButton *bt1 = [UIButton buttonWithType:UIButtonTypeSystem];

bt1.frame = CGRectMake(0, 0, 50, 30);

bt1.center = CGPointMake(self.view.center.x, self.view.frame.size.height - 50);

bt1.backgroundColor = [UIColor yellowColor];

[self.view addSubview:bt1];

[bt1 addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];

[bt1 setTitle:@"点击" forState:UIControlStateNormal];

//设置代理人

self.tf2.text = self.str;

}

//实现按钮的点击方法

- (void)click

{

//通过代理人使页面2 的text 通过协议方法 传到代理人

[self.delegate passValue:self.tf2.text];

//pop方法 是为了使页面2的值可以传回页面1

[self.navigationController popToRootViewControllerAnimated:YES];

}

上一篇下一篇

猜你喜欢

热点阅读