Swift: prepareForSegue with two different segues

13,366

Solution 1

Take out your IBActions (making sure to delete them in the Connections Inspector via the right side bar in the Interface Builder as well as in your code). For the segues, just use the Interface Builder, make sure both segue's identifiers are correct, then try this prepareForSegue method:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "matchSegue" {
        let controller = segue.destinationViewController as! ResultViewController
        controller.match = self.match
    } else if segue.identifier == "historySegue" {
        let controller = segue.destinationViewController as! HistoryViewController
        controller.history = self.history
    }
}

Solution 2

You could switch it...

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let identifier = segue.identifier {
        switch identifier {
        case "matchSegue":
            let controller = segue.destinationViewController as! ResultViewController
            controller.match = self.match
        case "historySegue":
            let controller = segue.destinationViewController as! HistoryViewController
            controller.history = self.history
        }
    }
}

Solution 3

You should compare the segue.identifier with the string not segue:

segue.identifier == "historySegue"

It won't fix your problem but it helps later.

I believe you have added an breakpoint and your app just stop at it. Remove the breakepoint and it will work.

Share:
13,366
Ragnar
Author by

Ragnar

Updated on June 13, 2022

Comments

  • Ragnar
    Ragnar almost 2 years

    I have 2 UIButtons on a view. Each of them are linked to 2 different views. They have to pass different data depending which button you just tapped.

    I know there is different way to segue but I want to use the prepareForSegue version for each (seems more clear for me to have the segue drawn on the storyboard and some code to explain what happened).

    I tried this...

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue != "historySegue") {
            let controller = segue.destinationViewController as! ResultViewController
            controller.match = self.match
        } else {
            let controller = segue.destinationViewController as! HistoryViewController
            controller.history = self.history
        }
    }
    
    
    @IBAction func showHistory(sender: UIButton) {
        performSegueWithIdentifier("historySegue", sender: self)
    }
    
    @IBAction func match(sender: UIButton) {
        performSegueWithIdentifier("matchSegue", sender: self)
    }
    

    But I got an error when I click the buttons

    (lldb)

  • Ragnar
    Ragnar almost 9 years
    I got : Binary operator ~= cannot be applied to operand of type String and String?
  • Fogmeister
    Fogmeister almost 9 years
    @Ragnar Ah, identifier is optional will update. Edited
  • Ragnar
    Ragnar almost 9 years
    Yes that's right I forgot this thanks. But onClick I still have this error Terminating app due to uncaught exception 'NSGenericException', reason: 'Could not find a navigation controller for segue 'historySegue'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.'
  • Ragnar
    Ragnar almost 9 years
    Thank you it works now ! I dont need and action using prepareForSegue. The storyboard handle the action via the segue !
  • Greg
    Greg almost 9 years
    It means that you choose Push segue but this kind of segue can be use with UINavigationControlle. Change the segue to modal or embed the view controller into navigation controller.