Search code examples
swiftsegueuistoryboardsegueibaction

Programming iOS button to do action on first tap then segue on second tap


I am building a 3rd party iOS app. As part of my main view controller, I give the user the option to press a button to link directly to the iOS keyboard settings so that they can enable my keyboard. When they return back to the app, it takes them to the next view where they can test the keyboard on an empty text field.

When the user hits the enable keyboard button on the app, I invoke an IBAction to link them to the settings app as such:

@IBAction func enableKeyboard(_ sender: Any) {
        let settingsUrl = URL(string: "\(UIApplicationOpenSettingsURLString)")!
        UIApplication.shared.open(settingsUrl)
    }

However, I concurrently have a segue to my textViewController so that when the user returns to the app from settings, they immediately go to the textViewController.

enter image description here

How would I be able to program my button so that the first tap takes them to settings, they return to the main view controller, and then the second tap takes them to the next view?


Solution

  • Flip the selected state of the button, and then test this when deciding what to do.

    @IBAction func touchUpInside(button: UIButton) {
        if !button.isSelected {
            button.isSelected = true
            // Now open settings
        } else {
           // Perform segue
        }
    }
    

    The button class is designed to keep track of its own state, so I believe this is preferable to having a variable keep track of the click in your view controller where only 2 states are required.