Reputation: 43579
I have a UIButton in a view. I attach an Event handler to it like this:
[self.button addTarget:self
action:@selector(button_touchUpInside:)
forControlEvents:UIControlEventTouchUpInside];
The handler looks like this:
-(void) button_touchUpInside:(id)sender
{
NSLog(@"%@", ((UIButton *)sender).enabled ? @"ENABLED" : @"DISABLED"); // Logs DISABLED
// Do stuff
}
I disable the button like this:
-(void)setEnabled:(BOOL)enabled
{
enabled_ = enabled;
self.button.enabled = enabled;
}
My problem is that even after I set enabled = NO
on the button a TouchUpInside
still triggers the handler. I can see in the handler that the button is disabled, however the handler is still triggered.
Please note that there are several ways of working around this - checking for button.enabled in the handler, @sanchitsingh's answer below etc. WHat I want to know is why this is happening.
Upvotes: 3
Views: 1482
Reputation: 632
From my personal experience, from this question, and others thread across the web, it seems that Apple's documentation about UIControl.enabled is incorrect, and that setting a UIControl disabled doesn't prevent it from getting touch events. It only neutralizes a few events such as (from memory, can't check now) click, touch down, and current action-triggering events, so you effectively have to use UserInteractionEnabled property to really get rid of touch events.
Upvotes: 4
Reputation: 1152
Just check if there is any gesture involved in your code. That could cause a problem. I think you should just use
button.enabled = NO;
button.userInteractionEnabled = NO;
Upvotes: 4