IBAction click notification from Button1 when clicking disabled Button2

I have two buttons of the same size (ie: Button1 and Button2). Button2 overlaps Button1 by 50% (see below).


If I click anywhere within Button2 then I receive an IBAction Click notification from Button2, as expected.

If I click anywhere within Button1 then I receive an IBAction Cick notification from Button1, as expected.


Issue

If Button2 is now disabled (ie: Button2.isEnabled = false) or user interaction set to false (ie: Button2.isUserInteractionEnabled = false) and I click on the left 50% of Button2 area (ie: the area which is overlapping Button1), then I will receive an IBAction Click notification from Button1


Questions

Why do I receive an IBAction Click notification from Button1 when clicking disabled Button2 ?


I would expect not to receive any IBAction Click notifications when clicking anywhere within the area of Button2


I think I can work around this issue in code, however, I was wondering whether this is normal handling and how others might resolve this behavior.

Accepted Reply

You wrote:

If Button2 is now disabled (ie: Button2.isEnabled = true)

I suppose you mean false


It is because:

- button2 disabled or interactionDisabled, so it does not intercept clicks (not in the responder chain)

- which pass through to button 1 (in responder chain)


However, why having overlapping buttons, except for testing and understanding (which is always interesting).

Replies

You wrote:

If Button2 is now disabled (ie: Button2.isEnabled = true)

I suppose you mean false


It is because:

- button2 disabled or interactionDisabled, so it does not intercept clicks (not in the responder chain)

- which pass through to button 1 (in responder chain)


However, why having overlapping buttons, except for testing and understanding (which is always interesting).

Sorry it took soooo long to reply, but I finally realized I could update my original post and delete various text one by one to get out of the "Waiting for Moderator" DRAFT state. It looks like this thread is officially posted now. I had to remove my manual diagram (contained text characters only) I provided showing how one card overlapped the other. I still do not know why that caused a moderator issue.


Anyway, I can now answer your questions ..


>>I suppose you mean false

Yes, I meant "false". I updated my original post


>>However, why having overlapping buttons,

IN an effort learn swift, I decided to make a card game. The cards in the player's hand overlap each other :>)

That makes sense !


Returning to your game, what do you expect when the card on top is disabled ?

In which conditions is button 2 (card 2) disabled ?

What type of action when tapping button 2 ?


I understand you would need button 1 to handle the click only when hit outside of button 2 ?

If so, I would try a different logic.

- keep button 2 enabled

- Instead of disabling, set for instance its tag to 0 vs 1 when enabled and adapt background color if needed

- when clicking on button, test the tag and return immediately if tag is 0, proceed if tag is 1.

>> I would try a different logic....

hehe ... I have already resolved the issue in my game by keeping track of which cards are not selectable and then ignoring the click event. I use an array as I am already using the tag value for other things. As a result, I never actully have to set "isEnabled = false" anymore for any cards. I was just curious as to whether swift(UIKit) was suppose to honor a click event which was made on a disabled button. You confirm the click event for that button is not generated, however, if another view is below it then that click event will be thrown.


That being said, let me address your questions ..


>>Returning to your game, what do you expect when the card on top is disabled ?

Clicking on a card would ultimately do nothing (ie: would not register any click event). Any part of another card, which located under a disabled card, thus is not physically "clickable" by the user, should not register a click event ever.


>>In which conditions is button 2 (card 2) disabled ?

The card game involves certain cards which cannot be selected at certain times during the game. The card is still in the player's hand but would be disabled so they would not be able to play/discard it.


>>What type of action when tapping button 2 ?

When tapping a disabled card occurs then nothing would happen ultimately. After all, the card is disabled ... right :>)

Sure there are many many ways to keep information about the state of a button or whatever object.


1. Tag is often convenient, because it is integrated in the button (that's more OO design). And by multiplexing information, one can hold easily several state information.


2. But you can also (as you did) declare an array to keep track of states (just be cautious if you remove cards to sync the array…


3. Or you could also crsubclass UIButton


class CardButton: UIButton {
    var isClickable : Bool = true
}


Which has the same benefit as 1, with more flexibility and kep tag available for other purpose.


Don't forget to close the thread.

Quick comment - under MVC programming you run a Model that is the brains of the game - it knows everything. You then use a Controller to display the appropriate View based on what is in the Model. In that formalism you would not 'store' information in the View elements like the button's enabled property or the tag value of the button. You let the dog wag the tail not the other way around. The advantage of MVC programming is that it allows you to easily port the game to different Views and it is more ammenable to changes made in the game logic or display as the programming advances.


Many of the back-and-forth comments and suggestions seem to be moving you to MVC structure.