iOS UIButton - Difference between UIButton setUserInteractionEnabled and setEnabled
Asked Answered
D

3

14

Wait!!!:
I know that you may think this question have been asked and answered several time before. But I can guarantee you that this question is unique.

Question:
In an iOS App, just imagine two buttons are there, like shown in the image bellow, and they have two actions which behaves like a toggling logic.

enter image description here

And it's logic may be as follows:

- (IBAction)testBtnClicked:(id)sender {
    if ([self.testBtn isEnabled]) {
        [self.testBtn setEnabled:NO];
        [self.setInteractionBtn setUserInteractionEnabled:YES];
    } else {
        [self.testBtn setEnabled:YES];
        [self.setInteractionBtn setUserInteractionEnabled:NO];
    }
}

- (IBAction)setInteractionBtnClicked:(id)sender {
    if ([self.setInteractionBtn isEnabled]) {
        [self.setInteractionBtn setUserInteractionEnabled:NO];
        [self.testBtn setEnabled:YES];
    } else {
        [self.setInteractionBtn setUserInteractionEnabled:YES];
        [self.testBtn setEnabled:NO];
    }
}

So I don't see a big difference between setEnabled method and setUserInteractionEnabled method. They same behave like a single method which blocks the user not letting use it. However if it is same alike, How could we be able to detect isEnabled true or false even when setUserInteractionEnabled is set to be False?

Following are the reasons which make this question not a possible duplicate of another Q&A Thread in SO:

  • Even though some high ranked code may have marked my question as a possible duplicate, that Q&A didn't give me the correct understanding.
  • As @danh said,

At least one reason is that during animation, user interaction is disabled on UIViews. It would be wrong for controls to draw themselves as greyed out while they are animated. So at least during animation, the two properties have distinct meanings.

Gave me the real answer or the reason to see that these two methods are for two reasons. Because anyone could say that setUserInteractionEnabled doesn't do changes on UI state, but at least only on @danh's answer had first stated that it might be implicitly used during UI Animations.

Derwood answered 4/12, 2015 at 4:55 Comment(2)
Possible duplicate of Which is better, setEnabled or setUserInteractionEnabled?Punt
Actually it's not. Because it's asking he knows the difference and that questionnaire just needed to know what's better out of both. But my question is I need to know what's the exact difference and which one to use. Hope you could understand it's two different questions. :-)Derwood
I
17

They are nearly the same. userInteractionEnabled is a property of UIView that toggles whether the view receives any user touches. enabled is a property of UIControl (which is a subclass of UIView and a superclass of UIButton) and has the same effect. One difference is that UIKit controls may draw themselves differently depending on their enabled state, which isn't the case for the abstract UIView.

Okay, then why?

Since UIControl subclasses inherit both, why are there two almost-the-same properties? Why don't controls just drop the idea of "enabled" and draw themselves differently based on their userInteractionEnabled state?

At least one reason is that during animation, user interaction is disabled on UIViews. It would be wrong for controls to draw themselves as greyed out while they are animated. So at least during animation, the two properties have distinct meanings.

Irresoluble answered 4/12, 2015 at 5:11 Comment(1)
One more imp point, suppose you have a button and you are trying to change the isSelected property of that button programmatically, say because you have two diff images for button bg. Now if you have set the button.isEnabled as false, and then try to changed button.isSelected , it wont work. But if you do, button. isuserInteractionEnabled = false and try to set the isselcted property it will wok.Zippel
G
4

Characteristics of enabled:

  • It's a property of UIControl
  • Superclass for UIButton.
  • It has effects on the visual state of the object and is generally the preferred method of disabling a control

Characteristics of userInteractionEnabled:

  • A property of UIView
  • Code that interacts with your controls is more likely to check if buttons are enabled than if their userInteractionEnabled property is set. It's more conventional.
Glassco answered 4/12, 2015 at 5:7 Comment(0)
P
1
@property(nonatomic, getter=isUserInteractionEnabled) BOOL userInteractionEnabled

A Boolean value that determines whether user events are ignored and removed from the event queue. When set to NO, user events—such as touch and keyboard—intended for the view are ignored and removed from the event queue. When set to YES, events are delivered to the view normally. The default value of this property is YES.

Discussion:

During an animation, user interactions are temporarily disabled for all views involved in the animation, regardless of the value in this property. You can disable this behavior by specifying the UIViewAnimationOptionAllowUserInteraction option when configuring the animation.

Apple Doc on UIView

@property(nonatomic, getter=isEnabled) BOOL enabled

A Boolean value that determines whether the receiver is 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.

For your reference:

  1. Apple Doc on UIControl
  2. SO Q&A

As @danh states:

"At least one reason is that during animation, user interaction is disabled on UIViews. It would be wrong for controls to draw themselves as greyed out while they are animated. So at least during animation, the two properties have distinct meanings."

Punt answered 4/12, 2015 at 10:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.