iOS Block的使用

开始学习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使用,希望对大家有帮助

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值