界面传值(协议)
import
import “SecondViewController.h”
@interface SecondViewController ()
@property(nonatomic,retain)UIButton *button;
@property(nonatomic,retain)UITextField *textField;
@end
@implementation SecondViewController
(void)dealloc
{
[_button release];
[_textField release];
[super dealloc];
}(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor yellowColor];self.title=@”第二页”;
self.button=[UIButton buttonWithType:UIButtonTypeSystem];
self.button.frame=CGRectMake(100, 200, 175, 50);
self.button.backgroundColor=[UIColor whiteColor];
[self.view addSubview:self.button];
[self.button setTitle:@”返回” forState:UIControlStateNormal];
self.button.layer.borderWidth=1;
self.button.layer.cornerRadius=10;
[self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];self.textField=[[UITextField alloc] initWithFrame:CGRectMake(100, 100, 175, 50)];
self.textField.backgroundColor=[UIColor redColor];
[self.view addSubview:self.textField];
[self.textField release];
self.textField.layer.borderWidth=1;
self.textField.layer.cornerRadius=10;
}
// 点击返回
// 协议的触发条件是点击按钮,所以在这里面进行协议传值的第三步
// 3.设置代理人执行的协议方法
-(void)click:(UIButton *)button{
[self.navigationController popViewControllerAnimated:YES];
[self.delegate changeValue:self.textField.text];
}
import “MainViewController.h”
import “SecondViewController.h”
// 4.签协议
@interface MainViewController ()
@property(nonatomic,retain)UILabel *label;
@property(nonatomic,retain)UIButton *button;
@end
@implementation MainViewController
(void)dealloc
{
[_label release];
[_button release];
[super dealloc];
}(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor redColor];
// 导航控制器的半透明变成不透明
self.navigationController.navigationBar.translucent=NO;self.title=@”第一页”;
self.label=[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 175, 50)];
self.label.backgroundColor=[UIColor yellowColor];
[self.view addSubview:self.label];
[self.label release];self.button=[UIButton buttonWithType:UIButtonTypeSystem];
self.button.frame=CGRectMake(100, 200, 175, 50);
self.button.backgroundColor=[UIColor whiteColor];
[self.view addSubview:self.button];
[self.button setTitle:@”下一页” forState:UIControlStateNormal];
self.button.layer.borderWidth=1;
self.button.layer.cornerRadius=10;
[self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)click:(UIButton *)button{
// push 下一页
SecondViewController *secVC=[[SecondViewController alloc] init];
// 5.设置代理人
secVC.delegate=self;
[self.navigationController pushViewController:secVC animated:YES];
[secVC release];
}
// 6.实现协议方法
-(void)changeValue:(NSString *)value{
self.label.text=value;
}