Jacksonkr
Jacksonkr

Reputation: 32207

iOS touch up inside on disabled button

If a UIButton has been disabled by [myButton setEnabled:NO]; is it possible to use a touch listener that still works?

I have tried:

[self addTarget:self action:@selector(myButtonTUI:) forControlEvents:UIControlStateDisabled];

but to no avail.

Upvotes: 2

Views: 4330

Answers (2)

devios1
devios1

Reputation: 38005

This can be achieved by wrapping the UIButton in a UIView of the same size and attaching a tap gesture recognizer to the parent view:

UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myButtonHit)];
[self.myButtonView addGestureRecognizer:gesture];

For some reason it doesn't seem to work when attaching the gesture recognizer to the UIButton itself, which would have been the nicest solution.

Upvotes: 2

ColdLogic
ColdLogic

Reputation: 7265

The answer to your question is No

enabled
A Boolean value that determines whether the receiver is enabled.

@property(nonatomic, getter=isEnabled) BOOL enabled
Discussion
Specify YES to make the control enabled; otherwise, specify NO to make it disabled. 
The default value is YES. If the enabled state is NO, the control ignores touch 
events and subclasses may draw differently.

Can you simulate such functionality? Yes.

Instead of disabling the button, make a check to see if the functionality should fire on the buttons action selector. You can even change the image to make it look disabled. This way the button still receives touch events, and you can fire the required functionality if the correct conditions are met.

Upvotes: 4

Related Questions