ControllerDelegate

#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.指定mainsecond的代理人

    

    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>


// 协议方法

// 参数: secondmain传参

// 返回值: miansecond返值

- (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;


}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值