Alicia I
Alicia I

Reputation: 13

Can a button have a two segues to two different view controllers / how can i code a view controller change without a segue?

This is what i have now, but it doesn't open the view controller. I dont think my let act1c = act1ViewController() works.

@IBAction func buttonPressed(_ sender: UIButton) {
    let act1vc = act1ViewController()

    let act2vc = activity2VC()

    switch sender.titleLabel?.text {
    case "cheer up":
     present(act1vc, animated: true, completion: nil)

    case "yay":
        present(act2vc, animated: true, completion: nil)

    default:
        break
    } 

Upvotes: 0

Views: 56

Answers (1)

vacawama
vacawama

Reputation: 154583

You can have 2 segues from one button, but then you'd have to conditionally cancel one in an override of func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool and that would be a confusing design.

I'd recommend wiring your two segues from the viewController icon at the top of the ViewController. Give them identifiers such as "segueToAct1" and "segueToAct2" in the Attributes Inspector and then trigger them with performSegue(withIdentifier:sender:):

@IBAction func buttonPressed(_ sender: UIButton) {

    switch sender.titleLabel?.text {
    case "cheer up":
        self.performSegue(withIdentifier: "segueToAct1", sender: nil)

    case "yay":
        self.performSegue(withIdentifier: "segueToAct2", sender: nil)

    default:
        break
    }
}

Upvotes: 1

Related Questions