Block传值
一般应用于逆向传值,即第二界面向第一界面传值,我们需要记住的两点:
1.要在第二个界面(SecondViewController.h)定义一个Block:
2.在第一个界面(ViewController.m)跳转第二个界面的方法中我们为block属性赋值完成block传值:
3.首先来到SecondViewController.h定义一个代码块
#import <UIKit/UIKit.h>
//定义Block
typedef void (^PushBlock)(NSString *);
//声明Block属性
@interface SecondViewController : UIViewController
@property (nonatomic,strong) PushBlock pushValueString;
@end
4.在SecondViewController,m里面设置UITextField 和 UIbutton 成为属性,然后在viewDidLoad 方法里面分别创建一个textField 和 一个button
#import "SecondViewController.h"
@interface SecondViewController ()
@property (nonatomic,strong) UITextField *textF;
@property (nonatomic,strong) NSString *textString;
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
_textF = [[UITextField alloc]initWithFrame:CGRectMake(0, 250, self.view.bounds.size.width, 50)];
_textF.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:_textF];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(20, 350, self.view.bounds.size.width-40, 50);
btn.backgroundColor = [UIColor purpleColor];
btn.titleLabel.font = [UIFont systemFontOfSize:25.0];
btn.tintColor = [UIColor whiteColor];
[btn setTitle:@"Back" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
5.实现button里面的点击事件的方法,并且在这方法里面实现Block的核心代码
-(void)backAction:(UIButton *)sender{
// 核心代码
_pushValueString (_textF.text);
[self dismissViewControllerAnimated:YES completion:nil];
}
6.回到ViewController里面,首先我们需要导入一下头文件#import “SecondViewController.h”,并且创建一个UITextField 和 UIbutton
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
@property (nonatomic,strong)UITextField *textField;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/**
* block传值(场景)一般用于逆向传值,即第二个界面传值给第一个界面
1.要在第二个界面(SecondViewController.h)定义一个Block:
2.要在第一个界面(ViewController.m)跳转第二个界面的方法中我们为block属性赋值完成block传值:
*/
self.view.backgroundColor = [UIColor greenColor];
self.title = @"Block传值";
_textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 250, self.view.bounds.size.width, 50)];
_textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:_textField];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(20, 350, self.view.bounds.size.width-40, 50);
btn.titleLabel.font = [UIFont systemFontOfSize:25.0];
btn.backgroundColor = [UIColor blueColor];
btn.tintColor = [UIColor whiteColor];
[btn setTitle:@"Next" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
7.实现一下button的点击事件的方法,并在这个方法里面实现跳转界面和block的核心代码属性赋值
-(void)nextAction:(UIButton *)sender{
SecondViewController *secondVC = [[SecondViewController alloc]init];
// 核心代码为block属性赋值
secondVC.pushValueString = ^(NSString *string){
_textField.text = string;
};
[self presentViewController:secondVC animated:YES completion:nil];
}
8.由于我们用的是模态视图进行页面之间的跳转,所以就不需要去创建UINavigationController了.模拟器运行效果如下