Leon Vladimirov
Leon Vladimirov

Reputation: 25

How to perform different segue based on condition from a button click in swift?

I'm building an app in which a button takes you to a profile registry view if you open the app for the first time and into another if you open the app any other time. How can I accomplish this programmatically, everything i have seen only connects the button to one view.

@IBAction func NextViewController(_ sender: UIButton) {
    if launchedBefore  {
        print("Not first launch.")

    } else {
        print("First launch, setting UserDefault.")


        UserDefaults.standard.set(true, forKey: "launchedBefore")
    }
}

Upvotes: 1

Views: 2936

Answers (1)

Jaydeep Vyas
Jaydeep Vyas

Reputation: 4470

I think, You are trying to connect the button to perform segue with multiple ViewController Right?? which is not possible

Instead that you have to connect segue between View Controllers

Adding a segue between two viewControllers:

From the Interface Builder, ctrl + drag between the two viewControllers that you want to link. You should see:

enter image description here

And now based on you condition you should perform segue with identifiers like below:

@IBAction func NextViewController(_ sender: UIButton) {
            if launchedBefore  {
        /*Use the Identifier you given in story Board*/
               self.performSegue(withIdentifier: "otherVC", sender: self)

            } else {
    /*Use the Identifier you given in story Board*/
        self.performSegue(withIdentifier: "register", sender: self))
                UserDefaults.standard.set(true, forKey: "launchedBefore")
            }
        }

For more Descriptive answer about segue see the answer How to connect two view controllers to one button in storyboard?

Upvotes: 14

Related Questions