我正在创建一个代表购物车的自定义UI按钮。如果用户的购物车中有商品,则该按钮会在右上角显示一个徽章,显示购物车中的商品数量。按钮和徽章的视觉外观工作正常。但是,在按钮上注册触摸事件不起作用。我已经阅读了关于这个主题的每个堆栈溢出问题,但找不到答案。
我正在使用以下库来绘制徽章:https://github.com/mikeMTOL/UIBarButtonItem-Badge
下面是我的自定义按钮类。在setup
方法中,我会听取点击。永远不会调用回调方法。
@interface BadgeCartButton()
- (void) setup;
- (UIImage *)_cartImage;
@end
@implementation BadgeCartButton
+ (instancetype)make {
// The badge itself is drawn relative to the frame of this view.
CGRect frame = CGRectMake(0, 0, [[self class] cartSize], [[self class] cartSize]);
return [[self alloc] initWithFrame:frame];
}
- (instancetype)init {
self = [super init];
[self setup];
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
[self setup];
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
[self setup];
return self;
}
+ (NSInteger)cartSize {
return 35;
}
- (NSAttributedString *)badgeAttributedTextForQuantityZero {
return nil;
}
- (BOOL)shouldDisplayBadgeForQuantityZero {
return NO;
}
- (UIFont *)badgeFont {
return [super badgeFont];
}
- (CGFloat)badgePadding {
return 3;
}
- (NSString *)badgeTextForQuantityZero {
// By default, no badge to show if the quantity is 0.
return nil;
}
- (UIColor *) badgeBackgroundColorForQuantityZero {
return nil;
}
- (UIColor *)badgeTextColorForQuantityZero {
return nil;
}
- (CGFloat)badgeOriginX {
return [super badgeOriginX] - 3;
}
- (CGFloat)badgeOriginY {
return [super badgeOriginY];
}
-(void)cartButtonWasTapped{
// This method is never called
}
- (void) setup {
[self addTarget:self action:@selector(cartButtonWasTapped) forControlEvents:UIControlEventTouchUpInside]; // listening here, never called
[self setImage:[self _cartImage] forState:UIControlStateNormal];
self.accessibilityLabel = @"cartButton";
self.accessibilityIdentifier = @"cartButton";
[self.KVOController observe:self keyPath:@"quantity" options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew action:@selector(updateBadge)];
self.quantity = @0;
self.shouldHideBadgeAtZero = ![self shouldDisplayBadgeForQuantityZero];
}
- (UIColor *)badgeBGColor {
return [MYTheme cautionColor];
}
- (UIColor *)badgeTextColor {
return [MYTheme lightTextColor];
}
+ (UIColor *)cartColor {
return [MYTheme actionColor];
}
- (UIImage *)_cartImage {
FAKIonIcons *cartIcon = [FAKIonIcons ios7CartOutlineIconWithSize:[[self class] cartSize]];
[cartIcon addAttribute:NSForegroundColorAttributeName value:[[self class] cartColor]];
CGSize size = CGSizeMake([[self class] cartSize], [[self class] cartSize]);
return [cartIcon imageWithSize:size];
}
- (void)updateBadge {
if ([_quantity integerValue] == 0) {
if ([self shouldDisplayBadgeForQuantityZero]) {
NSString *quantityZeroText = [self badgeTextForQuantityZero];
if (quantityZeroText) {
self.badgeValue = quantityZeroText;
} else {
NSAttributedString *quantityZeroAttributedText = [self badgeAttributedTextForQuantityZero];
if (quantityZeroAttributedText) {
self.attributedBadgeValue = quantityZeroAttributedText;
}
}
UIColor *quantityZeroBackgroundColor = [self badgeBackgroundColorForQuantityZero];
if (quantityZeroBackgroundColor) {
self.badgeBGColor = quantityZeroBackgroundColor;
}
UIColor *quantityZeroTextColor = [self badgeTextColorForQuantityZero];
if (quantityZeroTextColor) {
self.badgeTextColor = quantityZeroTextColor;
}
}
} else if (_quantity > 0) {
BOOL colorsMayHaveBeenChangedByQuantityZero = [self shouldDisplayBadgeForQuantityZero];
if (colorsMayHaveBeenChangedByQuantityZero) {
self.badgeBGColor = [self badgeBGColor];
self.badgeTextColor = [self badgeTextColor];
}
self.badgeValue = [self.quantity stringValue];
}
[self setNeedsDisplay];
}
@end
我花了3个小时阅读我能做的一切,没有结果。知道发生了什么事吗?
我正在通过在视图控制器中添加它来测试我的按钮,如下所示:
@implementation TestViewController
- (void)touched{
NSLog(@"TOUCH");
}
- (void)viewDidLoad {
[super viewDidLoad];
BadgeCartButton *btn = [BadgeCartButton make];
btn.frame = CGRectMake(100, 100, 60, 60);
[btn addTarget:self action:@selector(touched) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
@end
答案 0 :(得分:0)
请从设置方法下的Button Class中删除此行:
[self addTarget:self action:@selector(cartButtonWasTapped) forControlEvents:UIControlEventTouchUpInside]; //在这里听,从不叫
您可以在要添加此按钮对象的类中添加目标。
答案 1 :(得分:0)
更新:我清理过,一切正常。我喜欢XCode!