开始学习oc的时候也不是太懂Block的使用,后来慢慢的了解了一些,就把我知道的写出来吧
我写了一个关于block的测试demo
先展示效果图:
第一个界面:
第二个界面:
点击BACK回到第一个界面:
关键点 :
1.在将要传值的控制器中写一个block的属性
@property (nonatomic,strong) void(^ZQBlock)(NSArray *);
2.在将要传值的控制器点击BACK这个button的时候传值
NSArray *arry = @[@"1",@"2",@"这个是block的DEMO!"];
if (self.ZQBlock) {
self.ZQBlock(arry);
}
3.在第一个页面收到传过来的值
_secondVC = [[secondViewController alloc]init];
__weak typeof(self)weakSelf = self;
_secondVC.ZQBlock = ^(NSArray *arry){
weakSelf.textField.text = arry[2];
};
接下来是整个代码
第一个页面:ViewController
.h文件
#import <UIKit/UIKit.h>
#import "secondViewController.h"
@interface ViewController : UIViewController
@property (nonatomic,strong)secondViewController *secondVC;
@end
.m文件
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong)UITextField *textField;
@property (nonatomic,strong)UIButton *button;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.textField];
[self.view addSubview:self.button];
}
#pragma mark *** events ***
- (void)respondsToButton:(UIButton *)button{
[self presentViewController:self.secondVC animated:YES completion:nil];
}
#pragma mark *** lazy loading ***
-(UITextField *)textField{
if (!_textField) {
_textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, 40)];
_textField.backgroundColor = [UIColor yellowColor];
}
return _textField;
}
-(UIButton *)button{
if (!_button) {
_button = [UIButton buttonWithType:UIButtonTypeCustom];
_button.frame = CGRectMake(0, 0, 100, 40);
_button.center = self.view.center;
[_button addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];
_button.backgroundColor = [UIColor brownColor];
[_button setTitle:@"NEXT" forState:UIControlStateNormal];
}
return _button;
}
- (secondViewController *)secondVC{
if (!_secondVC) {
_secondVC = [[secondViewController alloc]init];
__weak typeof(self)weakSelf = self;
_secondVC.ZQBlock = ^(NSArray *arry){
weakSelf.textField.text = arry[2];
};
}
return _secondVC;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
第二个页面:secondViewController
.h文件
#import <UIKit/UIKit.h>
@interface secondViewController : UIViewController
@property (nonatomic,strong) void(^ZQBlock)(NSArray *);
@end
.m文件
#import "secondViewController.h"
@interface secondViewController ()
@property (nonatomic,strong)UIButton *backBtn;
@end
@implementation secondViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
[self.view addSubview:self.backBtn];
// Do any additional setup after loading the view.
}
#pragma mark *** events ***
- (void)respondsToBackButton:(UIButton *)button{
NSArray *arry = @[@"1",@"2",@"这个是block的DEMO!"];
if (self.ZQBlock) {
self.ZQBlock(arry);
}
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark *** lazy loading ***
- (UIButton *)backBtn{
if (!_backBtn) {
_backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_backBtn.frame = CGRectMake(0, 0, 100, 40);
_backBtn.center = self.view.center;
[_backBtn addTarget:self action:@selector(respondsToBackButton:) forControlEvents:UIControlEventTouchUpInside];
_backBtn.backgroundColor = [UIColor brownColor];
[_backBtn setTitle:@"BACK" forState:UIControlStateNormal];
}
return _backBtn;
}
- (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
这个demo只是简单的block使用,希望对大家有帮助