Day.03.11 Block关于TextField的互相传值应用

Block的基本运用, 适用于初学
ViewController.m

#import "ViewController.h"
#import "secondViewController.h"

@interface ViewController ()

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

@end

@implementation ViewController

- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    
    NSLog(@"视图将要出现");
}


- (IBAction)present:(UIButton *)sender {
    
    secondViewController *svc = [[secondViewController alloc]init];
    
    [svc setBlock:^(NSString *text) {
       
        if (text.length >0) {
            
            self.tf.text = text;
        }
        
        return _tf.text;
    }];
    
    [self presentViewController:svc animated:YES completion:^{
       
        NSLog(@"你是一只小小狗,你是个骨头");
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

secondViewController.h

#import 

typedef NSString *(^TextBlock)(NSString *);

@interface secondViewController : UIViewController

@property (nonatomic,copy)TextBlock block;

- (void)setBlock:(TextBlock)block;


@end

secondViewController.m

#import "secondViewController.h"

@interface secondViewController ()
@property (weak, nonatomic) IBOutlet UITextField *tf;

@end

@implementation secondViewController

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    _tf.text=_block(NULL);
}
- (IBAction)dismiss:(UIButton *)sender {
    
    _block(_tf.text);
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        NSLog(@"只有我才能带领你们走向胜利");
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

Day.03.11 Block关于TextField的互相传值应用_第1张图片
屏幕快照 2016-03-11 下午8.13.17.png
Day.03.11 Block关于TextField的互相传值应用_第2张图片
屏幕快照 2016-03-11 下午8.13.28.png

你可能感兴趣的:(Day.03.11 Block关于TextField的互相传值应用)