block 在两个控制器之间传值使用

来源:互联网 发布:企业级备份软件排名 编辑:程序博客网 时间:2024/05/22 02:08

转自:

除了代理传值的事件 ,还有一种可以更为简便的方法 ,就是block 传值,利用block 传值 在控制器跳转的瞬间将值传递。

例子:将Vc1 控制器接收传值 ,Vc2控制器发送传值消息,为了便于区别和代理传值的 使用 , 将两者放到 一起其中Vc1控制器里 两个UITextField一个接收代理传值的使用,一个接收block 的使用 。
Vc1 :


#import "ViewController.h"

#import "SecondViewController.h"

@interface ViewController ()<</span>secondViewControllerDelegate>

- (IBAction)gengxin;

@property (weak, nonatomic) IBOutlet UITextField *textField;

@property (weak, nonatomic) IBOutlet UITextField *secTextField;

@property (nonatomic,strong) SecondViewController *secVc;

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

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

    [secondVc returnText:^(NSString *showText) {

        NSLog(@"---------%@------------",showText);

        self.secTextField.text = showText;

    }];

    self.secVc = secondVc;

    self.secVc.delegate = self;

    

}

- (IBAction)gengxin {

    [self presentViewController:self.secVc animated:YES completion:nil];

}

#pragma mark - secondViewControllerDelegate

- (void)setTextfield:(NSString *)text{

    self.textField.text = text;

    NSLog(@"%@",text);

}

@end

Vc2 :

#import "ViewController.h"

@protocol secondViewControllerDelegate

@optional - (void)setTextfield:(NSString *)text;

@end


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

@interface SecondViewController : ViewController

@property (nonatomic,strong) id delegate;

//声明block 属性

@property (nonatomic,strong)returnBlock returnTextBlock;

//声明 调用方法

- (void) returnText: (returnBlock)block;

@end


 

#import "SecondViewController.h"


@interface SecondViewController ()

@property (nonatomic,weak) UITextField *textField;

@property (nonatomic,weak) UIButton *saveButton;

@end


@implementation SecondViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor grayColor];

    UITextField *textField =[[UITextField allocinit];

    [self.view addSubview:textField];

    [textField setBorderStyle:UITextBorderStyleRoundedRect];

    self.textField = textField;

    self.textField.frame = CGRectMake(100100100100);

    UIButton *save = [[UIButton allocinit];

    [self.view addSubview:save];

    self.saveButton = save;

    self.saveButton.frame = CGRectMake(20020010070);

    [self.saveButton setTitle:@"确定" forState:UIControlStateNormal];

    self.saveButton.titleLabel.textColor = [UIColor blackColor];

    [self.saveButton addTarget:self action:@selector(buttonClick:)forControlEvents:UIControlEventTouchUpInside];

    

}

- (void) buttonClick:(id)sender{

    if ([self.delegate respondsToSelector:@selector(setTextField:)]) {

        NSString *text =self.textField.text;

        [self.delegate setTextfield:text];

    }

    [self dismissViewControllerAnimated:YES completion:nil];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

    

}

- (void) returnText:(returnBlock)block

{

    self.returnTextBlock = block;

}

- (void)viewDidDisappear:(BOOL)animated

{

    //即将消失的时候

    if (self.returnTextBlock !=nil) {

        self.returnTextBlock(self.textField.text);

    }

    NSLog(@"======%@=======",self.returnTextBlock);

}

 

@end


两者之间 的理解方式相同 ,在Vc1传值的里面,将returenText:方法 里的 block值传入 vc2控制器里,在控制器Vc2跳转的时候 ,可以判断属性里有值 ,然后再重新加载vc1的时候 就可以将值写入控制器里的secTextField里了。

0 0
原创粉丝点击