iOS开发 使用Block实现两个页面互相传值

RootViewController.m里


#import "RootViewController.h"

#import "SecondViewController.h"

@interface RootViewController ()

@property(nonatomic, retain)UITextField *textField;

@property(nonatomic, retain)UILabel *label;

@end


@implementation RootViewController


- (void)dealloc

{

    [_label release];

    [_textField release];

    [super dealloc];

}

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    _label = [[UILabel alloc]initWithFrame:CGRectMake(100, 200, 100, 30)];

    _label.text = @"YES";

    [self.view addSubview:_label];

    

    _textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 30)];

    _textField.borderStyle = UITextBorderStyleRoundedRect;

    [self.view addSubview:_textField];

    

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

    button.frame = CGRectMake(100, 300, 100, 30);

    [button setTitle:@"OK" forState:UIControlStateNormal];

    [button addTarget:self action:@selector(didClick:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button];

}

- (void)didClick:(UIButton *)button

{

    SecondViewController *secondVC = [[SecondViewController alloc]init];

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

    secondVC.myblock = ^(NSString *string){

        _label.text = string;

    };

    

    secondVC.passValue = ^(UILabel *label)

    {

        label.text = self.textField.text;

    };

    [secondVC release];

}

@end


SecondViewController.h里

#import <UIKit/UIKit.h>

typedef void(^returnBlock)(NSString *);

typedef void(^passvalueBlock)(UILabel *);

@interface SecondViewController : UIViewController

@property(nonatomic, copy)returnBlock myblock; // 第二个页面传给第一个页面

@property(nonatomic, copy)passvalueBlock passValue;  // 第一个页面传给第二个页面

@end


SecondViewController.m里

#import "SecondViewController.h"

@interface SecondViewController ()

@property(nonatomic, retain)UITextField *textField;

@property(nonatomic, retain)UILabel *label;

@end


@implementation SecondViewController


- (void)dealloc

{

    [_label release];

    [_textField release];

    [super dealloc];

}

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    self.view.backgroundColor = [UIColor whiteColor];

    _label = [[UILabel alloc]initWithFrame:CGRectMake(50, 200, 100, 30)];

    self.passValue(_label);

    [self.view addSubview:_label];

    

    _textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 200, 30)];

    _textField.borderStyle = UITextBorderStyleRoundedRect;

    [self.view addSubview:_textField];

    

    

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

    button.frame = CGRectMake(50, 300, 100, 30);

    [button setTitle:@"OK" forState:UIControlStateNormal];

    [self.view addSubview:button];


}


- (void)viewWillDisappear:(BOOL)animated

{

    [super viewWillDisappear:animated];

    self.myblock(self.textField.text);

    

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值