您的位置:首页 > 移动开发 > IOS开发

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

2015-09-05 11:30 651 查看
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);
    
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios block传值