I think there are better solutions to this problem than what you've proposed, but to answer your question directly: A subclass of UIButton observes touch events the same way that everyone else observes touch events.
// In your UIButton subclass- (instancetype)initWithFrame:(CGRect)frame { self = [super buttonWithType:UIButtonTypeCustom]; if (self) { [self addTarget:self action:@selector(didTouchButton) forControlEvents:UIControlEventTouchUpInside]; } return self;}- (void)didTouchButton { // do whatever you need to do here}
Important note: you can't use [UIButton buttonWithType:]
to create your button, you've got to use init
or initWithFrame:
. Even though UIButton has the convenience initializer, initWithFrame:
is still the designated initializer.