Swift: prepareForSegue with two different segues
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.
Ragnar
Updated on June 13, 2022Comments
-
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 theprepareForSegue
version for each (seems more clear for me to have thesegue
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 almost 9 yearsI got :
Binary operator ~= cannot be applied to operand of type String and String?
-
Fogmeister almost 9 years@Ragnar Ah, identifier is optional will update. Edited
-
Ragnar almost 9 yearsYes 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 almost 9 yearsThank you it works now ! I dont need and action using prepareForSegue. The storyboard handle the action via the segue !
-
Greg almost 9 yearsIt 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.