场景一: 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的使用,自我感觉比起通知代理要方便的多,有疑问的小伙伴可以留言问我。