John Durand John Durand - 2 months ago 307
Swift Question

swift 3 prepare(for segue: ) function broken?

for some odd reason , with swift 3, the

prepare(for segue:
method refuses to acknowledge the segue identifier. I have the following IBAction's connected to a couple button's on the UI:

@IBAction func goToImagesPicker(_ sender: AnyObject) {
performSegue(withIdentifier: "showImagePicker", sender: sender)

}

@IBAction func goToNamePicker(_ sender: AnyObject) {
performSegue(withIdentifier: "showNamePicker", sender: sender)
}


However in my
prepare(for segue:
method, it doesn't recognize the different segue identifier's, I know so because my console doesn't log the messages I assigned to each:

func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {

if segue.identifier == "showImagePicker" {

print("This is the Image Picker")

}

if segue.identifier == "showNamePicker" {

print("This is the Name Picker")

}
}


any suggestions? or is this just a bug?

dan dan
Answer

Your method isn't getting called at all because you have the wrong signature. It was changed in Xcode 8 beta 6 to:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

Note that the type of sender is Any? instead of AnyObject?. You should have had an error after upgrading Xcode which told you your method wasn't overriding any method from its superclass which should have clued you in before you deleted the override.