最直接的教你OC中Block的簡單使用場景


場景一: A控制器跳轉到B控制器   --   B控制器事件處理通過Block回調給A控制器

A 跳轉前界面如下

 

點擊ToB按鈕到控制器B

 

在控制器B中點擊按鈕返回到A界面如下

                             

 

 

 

不說廢話上碼!!!!

 A-->控制器 .m

#import "ViewControllerA.h"
#import "ViewControllerB.h"

@interface ViewControllerA ()

@property (nonatomic, strong) ViewControllerB *controllerB;

@property (nonatomic, strong) UILabel *testLabel;

@end

@implementation ViewControllerA

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *jumpButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 80, 100, 100)];
    jumpButton.backgroundColor = [UIColor redColor];
    jumpButton.layer.cornerRadius = 50;
    [jumpButton addTarget:self action:@selector(jumpButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    [jumpButton setTitle:@"ToB" forState:UIControlStateNormal];
    [self.view addSubview:jumpButton];
    
    _testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 40, 40)];
    _testLabel.center = self.view.center;
    _testLabel.textAlignment = NSTextAlignmentCenter;
    _testLabel.backgroundColor = [UIColor lightGrayColor];
    _testLabel.textColor = [UIColor redColor];
    [self.view addSubview:_testLabel];
}

- (void)jumpButtonClickAction {
    
    //TODO: 聲明weakSelf 在Block中使用 方式Block循環引用
    __weak typeof(self) weakSelf = self;
    _controllerB = [[ViewControllerB alloc] init];
    
    //TODO: 控制器中點擊測試按鈕 通過Block的回調實現
    _controllerB.change_controllerA_labelTitleBlock = ^(NSString *title) {
        weakSelf.testLabel.text = [NSString stringWithFormat:@"點擊了%@",title];
    };
    
    [self.navigationController pushViewController:_controllerB animated:YES];
}

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

@end

 

B--> .h代碼

#import <UIKit/UIKit.h>

@interface ViewControllerB : UIViewController

//TODO: 聲明用來回調的 Block
@property (nonatomic, copy) void(^change_controllerA_labelTitleBlock)(NSString *title);

@end

 

B--> .m代碼

#import "ViewControllerB.h"

@interface ViewControllerB ()

@end

@implementation ViewControllerB

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *buttonLeft = [[UIButton alloc] initWithFrame:CGRectMake(40, 100, 100, 100)];
    buttonLeft.backgroundColor = [UIColor blackColor];
    [buttonLeft setTitle:@"buttonLeft" forState:UIControlStateNormal];
    [buttonLeft addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonLeft];
    
    UIButton *buttonRight = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(buttonLeft.frame) + 40, 100, 100, 100)];
    buttonRight.backgroundColor = [UIColor blackColor];
    [buttonRight setTitle:@"buttonRight" forState:UIControlStateNormal];
    [buttonRight addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonRight];
    
}

- (void)buttonClickAction:(UIButton *)sender {
    
    //TODO: 判斷 self.change_controllerA_labelTitleBlock 是否為空(必寫)
    if (self.change_controllerA_labelTitleBlock) {
        
        //TODO: Block 會調給控制器A 值
        self.change_controllerA_labelTitleBlock(sender.titleLabel.text);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

//TODO: Block Set方法(必寫)
- (void)setChange_controllerA_labelTitleBlock:(void (^)(NSString *))change_controllerA_labelTitleBlock {
    _change_controllerA_labelTitleBlock = change_controllerA_labelTitleBlock;
}

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


@end

 

場景二:A控制器中添加子View 子View中的事件回調 

截圖如下

A --> .m

#import "ViewControllerA.h" #import "TestBlockView.h" @interface ViewControllerA () @property (nonatomic, strong) TestBlockView *testBlockView; @property (nonatomic, strong) UILabel *testLabel; @end @implementation ViewControllerA - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor whiteColor]; _testLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 70, self.view.frame.size.width - 40, 40)]; _testLabel.textAlignment = NSTextAlignmentCenter; _testLabel.backgroundColor = [UIColor lightGrayColor]; _testLabel.textColor = [UIColor redColor]; [self.view addSubview:_testLabel]; __weak typeof(self) weakSelf = self; _testBlockView = [[TestBlockView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 100, self.view.frame.size.width - 100) testBlock:^(NSString *title) { weakSelf.testLabel.text = [NSString stringWithFormat:@"點擊了%@",title]; }]; _testBlockView.center = self.view.center; _testBlockView.backgroundColor = [UIColor yellowColor]; [self.view addSubview:_testBlockView]; } @end

 

TestBlockView --> .h

#import <UIKit/UIKit.h>

//TODO: 聲明Block的第二種寫法

typedef void(^TestBlock)(NSString *title);

@interface TestBlockView : UIView

@property (nonatomic, copy) TestBlock testBlock1;


//TODO: 重寫init方法 加上Block回調處理
- (instancetype)initWithFrame:(CGRect)frame testBlock:(TestBlock)testBlock1;
@end

 

TestBlockView --> .m

#import "TestBlockView.h"

@implementation TestBlockView

//TODO: 重寫init方法 加上Block回調處理
- (instancetype)initWithFrame:(CGRect)frame testBlock:(TestBlock)testBlock1 {
    if (self = [super initWithFrame:frame]) {
        _testBlock1 = testBlock1;
        [self addTestButton];
    }
    return self;
}

- (void)addTestButton {
    UIButton *testButton1 = [[UIButton alloc] initWithFrame:CGRectMake(50, 100, 100, 100)];
    testButton1.layer.cornerRadius = 20;
    testButton1.backgroundColor = [UIColor lightGrayColor];
    [testButton1 setTitle:@"testButton1" forState:UIControlStateNormal];
    [testButton1 addTarget:self action:@selector(testButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:testButton1];
    
    UIButton *testButton2 = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(testButton1.frame) + 10, CGRectGetMinY(testButton1.frame), 100, 100)];
    testButton2.layer.cornerRadius = 20;
    testButton2.backgroundColor = [UIColor lightGrayColor];
    [testButton2 setTitle:@"testButton2" forState:UIControlStateNormal];
    [testButton2 addTarget:self action:@selector(testButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:testButton2];
}

- (void)testButtonClick:(UIButton *)sender {
    if (self.testBlock1) {
        self.testBlock1(sender.titleLabel.text);
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

 

以上是我簡單直接的介紹了項目中常用的兩種Block的使用,自我感覺比起通知代理要方便的多,有疑問的小伙伴可以留言問我。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM