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

来源:互联网 发布:淘宝客拉人广告词 编辑:程序博客网 时间:2024/06/09 17:11

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

{

    [_labelrelease];

    [_textFieldrelease];

    [superdealloc];

}

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    

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

    _label.text =@"YES";

    [self.viewaddSubview:_label];

    

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

    _textField.borderStyle =UITextBorderStyleRoundedRect;

    [self.viewaddSubview:_textField];

    

    UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];

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

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

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

    [self.viewaddSubview:button];

}

- (void)didClick:(UIButton *)button

{

    SecondViewController *secondVC = [[SecondViewControlleralloc]init];

    [self.navigationControllerpushViewController: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

{

    [_labelrelease];

    [_textFieldrelease];

    [superdealloc];

}

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    

    self.view.backgroundColor = [UIColorwhiteColor];

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

    self.passValue(_label);

    [self.viewaddSubview:_label];

    

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

    _textField.borderStyle =UITextBorderStyleRoundedRect;

    [self.viewaddSubview:_textField];

    

    

    UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];

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

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

    [self.viewaddSubview:button];


}


- (void)viewWillDisappear:(BOOL)animated

{

    [superviewWillDisappear:animated];

    self.myblock(self.textField.text);

    

}


0 0
原创粉丝点击