#import "MainViewController.h"
#import "SecondViewController.h"
#warning 协议传值: 4. 在main里面签订协议
@interface MainViewController ()<SecondViewControllerDelegate>
@property (nonatomic,retain) UIButton *button;
@end
@implementation MainViewController
- (void)dealloc
{
[_button release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"第一页";
self.view.backgroundColor = [UIColor lightGrayColor];
self.button = [UIButton buttonWithType:UIButtonTypeSystem];
_button.frame = CGRectMake(20, 120, 335, 50);
_button.backgroundColor = [UIColor yellowColor];
[_button setTitle:@"button" forState:UIControlStateNormal];
[_button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_button];
}
- (void)buttonAction:(UIButton *)btn
{
SecondViewController *secondVC = [[SecondViewController alloc] init];
// 给第二个页面的text属性赋值
secondVC.text = btn.currentTitle;
#warning 协议传值: 5.指定main为second的代理人
secondVC.delegate = self;
[self.navigationController pushViewController:secondVC animated:YES];
[secondVC release];
}
#warning 协议传值: 6. 在main里面实现协议方法
- (void)passValue:(NSString *)text
{
// 利用text参数给button传值
[_button setTitle:text forState:UIControlStateNormal];
}
#import <UIKit/UIKit.h>
#warning 协议传值: 1.由后面的页面(不干活的)创建一个协议
@protocol SecondViewControllerDelegate <NSObject>
// 协议方法
// 参数: second给main传参
// 返回值: mian给second返值
- (void)passValue:(NSString *)text;
@end
@interface SecondViewController : UIViewController
// 用来接收外部的字符串对象
@property (nonatomic,retain) NSString *text;
#warning 协议传值: 2. 创建一个代理人属性delegate,方便second指挥代理人执行方法
@property (nonatomic,assign) id<SecondViewControllerDelegate> delegate;
@end
#import "SecondViewController.h"
@interface SecondViewController ()<UITextFieldDelegate>
@property (nonatomic,retain) UITextField *textField;
@end
@implementation SecondViewController
- (void)dealloc
{
[_text release];
[_textField release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"第二页";
self.view.backgroundColor = [UIColor redColor];
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 120, 335, 50)];
// 使用外部给的数据对象,给自己的控件赋值
self.textField.text = self.text;
_textField.borderStyle = UITextBorderStyleRoundedRect;
_textField.backgroundColor = [UIColor yellowColor];
[self.view addSubview:_textField];
[_textField release];
_textField.delegate = self;
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(20, 220, 335, 50);
button.backgroundColor = [UIColor yellowColor];
[button setTitle:@"返回" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonAction:(UIButton *)btn
{
#warning 协议传值: 3. 在second中的合适的地方,让代理人执行协议方法
[self.delegate passValue:_textField.text];
[self.navigationController popViewControllerAnimated:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[_textField resignFirstResponder];
return YES;
}