IOS传值之Block传值(二)

@interface QWViewController : UIViewController

@property(nonatomic,strong)UILabel *label;

1@property(nonatomic,strong)UITextField *textField;

@end

#import "QWViewController.h"

#import "QWViewControllerTwo.h"

 

@interface QWViewController ()

@end

@implementation QWViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];

    label.backgroundColor=[UIColor blueColor];

    self.label=label;

    [self.view addSubview:label];

    

    UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(100, 200, 100, 50)];

    textField.backgroundColor=[UIColor yellowColor];

    self.textField=textField;

    [self.view addSubview:textField];

    

    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];

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

    button.backgroundColor=[UIColor blackColor];

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

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

    [self.view addSubview:button];

}

-(void)doClicked{

    QWViewControllerTwo *ViewControllerTwo=[[QWViewControllerTwo alloc]init];

    ViewControllerTwo.labelText=self.textField.text;//顺传值

    //[ViewControllerTwo.label setText:self.textField.text];//注意:这样传值是不对的。

    //逆向传值回来

    [ViewControllerTwo returnText:^(NSString *showText) {

        self.label.text=showText;

    }];

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

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

#import <UIKit/UIKit.h>

 typedef void(^ReturnTextBlock) (NSString *showText);

 @interface QWViewControllerTwo : UIViewController

@property(nonatomic,copy)NSString *labelText;

@property(nonatomic,strong)UILabel *label;

@property(nonatomic,strong)UITextField *textField;

@property(nonatomic,copy)ReturnTextBlock returnTextBlock;

-(void)returnText:(ReturnTextBlock)block;

@end

#import "QWViewControllerTwo.h"

 

@interface QWViewControllerTwo ()

 

@end

 

@implementation QWViewControllerTwo

 

-(UILabel *)label{

    if (_label==nil) {

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

    }

    return _label;

}

-(UITextField *)textField{

    if (_textField==nil) {

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

    }

    return _textField;

}

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];

    label.backgroundColor=[UIColor yellowColor];

    label.text=self.labelText;

    self.label=label;

    [self.view addSubview:self.label];

    

    UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(100, 200, 100, 50)];

    textField.backgroundColor=[UIColor yellowColor];

    self.textField=textField;

    [self.view addSubview:textField];

    //创建返回按钮

    UIButton *backBtn=[UIButton buttonWithType:UIButtonTypeCustom];

    [backBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];

    [backBtn setTitle:@"back" forState:UIControlStateNormal];

}

-(void)goBack{

    [self.navigationController popViewControllerAnimated:YES];

}

//将上一个控制器传过来的block保存在本控制器中,在合适的时候调用

-(void)returnText:(ReturnTextBlock)block{

    self.returnTextBlock=block;

}

//在视图将要消失的时候调用本类的block

-(void)viewWillDisappear:(BOOL)animated{

    if (self.returnTextBlock !=nil) {

        self.returnTextBlock(self.textField.text);//实现是在之前控制器中

    }

    

}

@end

实现效果:

 

 

原文地址:https://www.cnblogs.com/Jordandan/p/5046753.html