我们在写子view和自定义cell的时候,常常会用到要和conllection去做交互,这时候会需要在collection中实现一些view的button的点击方法就需要用到回调事件,我们常用的回调方法一般都是两种Block和代理-Delegate
Block:
@property (nonatomic, copy)void(^selectFlightConditionBlock)();
// 在btn点击事件里面写
if (self.backToRuleActionBlock) {
self.backToRuleActionBlock();
}
//回调
// 写
@property(nonatomic, copy)void(^bookingActionBlock)();
// 实现
if (self.bookingActionBlock) {
self.bookingActionBlock();
}
// 调用
cell.bookingActionBlock = ^{};
@property (nonatomic, copy) void(^changeDeliveryCityBlock)(void);
- (IBAction)modifyCityAction:(UIButton *)sender {
if (self.changeDeliveryCityBlock) {
self.changeDeliveryCityBlock();
}
}
Delegate:
@protocol ShoppingCartListHeaderDelegete <NSObject>
// 选择
- (void)teamSelected:(BOOL)isSelected indexSection:(NSInteger)indexSection;
@end
@interface HXDShoppingCartListHeaderView : UIView
@property (nonatomic, assign) id<ShoppingCartListHeaderDelegete> delegate;
@end
- (IBAction)selectedAction:(UIButton *)sender {
sender.selected = !sender.selected;
if (self.delegate && [self.delegate respondsToSelector:@selector(teamSelected: indexSection:)]) {
[self.delegate teamSelected:sender.selected indexSection:self.indexSection];
}
}