Ben Lu
Ben Lu

Reputation: 3042

How could a disabled UIButton trigger another event?

Well, I want to give a warning when users try to click a DISABLED UIButton.

How can I catch the event of a disabled button being clicked?

Upvotes: 0

Views: 863

Answers (1)

stevex
stevex

Reputation: 5827

Not saying it's good design (I agree with the commenters who say that a tap on a disabled button shouldn't do anything), but you could attach a UITapGestureRecognizer to the parent view, and when the gesture comes in, check to see if the tap is within the bounds of the disabled view.

Taps on the disabled button will fire a UITapGestureRecognizer that's attached to the button's superview.

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
[self.view addGestureRecognizer:tapRecognizer];

and then in viewTapped

- (void)viewTapped:(id)sender {
    UITapGestureRecognizer *recognizer = (UITapGestureRecognizer *)sender;
    CGPoint pt = [recognizer locationOfTouch:0 inView:self.testButton];
    if (CGRectContainsPoint(self.testButton.bounds, pt)) {
        NSLog(@"Disabled button tapped");
    }
}

Upvotes: 3

Related Questions