Search code examples
iosobjective-cuiswitchtouch-up-inside

Detect Switch touched before changing value


I have a uiswitch in settings that allows users to view some sensitive things in the app. Before allowing users to change the switch through a tap I want to prompt them to provide a password.

I tried disabling the switch but it's impossible (without some kludgy workarounds) to detect a tap/touch from a disabled switch.

Is there an event or way to detect the touch/tap before the uiswitch changes value so that I can prevent it from changing value and show the prompt?

I tried using touchdown and touch inside but they don't stop the change in value.

Thanks for any suggestions.


Solution

  • Here's an idea. Disable the switch as you said already. But also have another view behind the switch, a view which is the same size as the switch and has a UITapGestureRecognizer behind it.

    Do you see what will happen? If the switch is disabled, the touch will fall thru to the view behind it, and the tap gesture recognizer will fire. Thus you will know that the user tried to tap the switch.

    But once the password has been given and the switch is enabled, you can ignore the tap gesture recognizer (in fact it will probably never fire again, because the switch, being in front of it, will eat the touch).