title: Can not tap custom UIButton
body: <p>I'm creating a custom UI button representing a cart. If the user's cart has items in it, the button displays a badge over the top right corner showing the number of items in the cart. The visual appearance of the button and the badge are working fine. However, registering for touch events on the button doesn't work. I've read every stack overflow question on the subject and can't find an answer.</p>
  
  <p>I'm using the following library to draw the badge: <a href="https://github.com/mikeMTOL/UIBarButtonItem-Badge" rel="nofollow">https://github.com/mikeMTOL/UIBarButtonItem-Badge</a></p>
  
  <p>Below is my custom button class. Inside the <code>setup</code> method, I listen for taps. The callback method is never invoked.</p>
  
  <pre><code>@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 &gt; 0) {
          BOOL colorsMayHaveBeenChangedByQuantityZero = [self shouldDisplayBadgeForQuantityZero];
  
          if (colorsMayHaveBeenChangedByQuantityZero) {
              self.badgeBGColor = [self badgeBGColor];
              self.badgeTextColor = [self badgeTextColor];
          }
  
          self.badgeValue = [self.quantity stringValue];
      }
      [self setNeedsDisplay];
  }
  
  @end
  </code></pre>
  
  <p>I've spent 3 hours reading everything I can with no results. Any idea what's going on?</p>
  
  <p>I'm testing my button by adding it in a View Controller like so:</p>
  
  <pre><code>@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
  </code></pre>