3

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.

Undistraction
  • 42,754
  • 56
  • 195
  • 331

3 Answers3

4

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;
khushalbokadey
  • 1,132
  • 1
  • 11
  • 25
4

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.

Community
  • 1
  • 1
psycho
  • 631
  • 10
  • 27
1

Try this

button.userInteractionEnabled=NO;
Sanchit Paurush
  • 6,114
  • 17
  • 68
  • 107