View1 按鈕點擊 → 回調 View2 方法,傳遞參數。
// View1.h
typedef void (^ButtonActionBlock)(NSInteger tag); // 簡化 Block 類型
@property (nonatomic, copy) ButtonActionBlock actionBlock;
// View1.m 按鈕事件
- (IBAction)btnClick:(UIButton *)sender {
if (self.actionBlock) self.actionBlock(sender.tag);
}
// View2.m 創建 View1 並設置回調
- (void)setupView1 {
View1 *v1 = [[View1 alloc] init];
v1.actionBlock = ^(NSInteger tag) {
// 這裡處理 View1 按鈕點擊,tag 為按鈕 tag
[self handleButtonTag:tag];
};
[self.view addSubview:v1];
}