Prepare(For Segue: Uistoryboardsegue, Sender: Anyobject) Missing in Swift 3.0/Xcode 8 B6

prepare(for segue: UIStoryboardSegue, sender: AnyObject?) missing in swift 3.0/Xcode 8 b6

Maybe this was a bug in Beta 6. I'm using the GM seed, and it is working:

prepareforsegue

BTW: In beta 6, the method was renamed to

prepare(for segue: UIStoryboardSegue, sender: Any?)

swift 3 prepare(for segue: ) function broken?

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.

Prepare For Segue With Array - Xcode 8.0 Swift 3.0

I am pretty new to iOS/Swift but I recently ran into the same situation. Here is how I do it.

SourceViewController.swift

class SourceViewController: UIViewController {
let stringToPass = "Hello World"

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! DestinationViewController
destinationVC.receivedString = stringToPass
}
}

DestinationViewController.swift

class DestinationViewController: UIViewController {

var receivedString: String?

if let newString = receivedString {
print(newString)
}
...

I realize this is slightly different than your example, but the important thing to note is that when you create "destinationVC" you are then able to modify the properties of it. The key difference is you have to provide the scope of the variable (destinationVC.receivedString) when assigning a value or in your case appending to an array:

destViewController.items.append(textField.text!)

Without providing the scope Xcode is unable to find the variable (identifier) you are trying to modify since it wasn't part of the current file or part of an import.

prepare(for segue: UIStoryboardSegue, sender: Any?) not called in Xcode 9 b6 on iOS 11

Just a clean build solved this for me in the end.

prepare(for:sender:) not getting called

The method signature has changed. sender is now Any? instead of AnyObject?

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

This is to coincide with the changes to how Swift is bridged with obj-c, described here under "New in Xcode 8 beta 6 - Swift Compiler"

Swift 2 to 3 Migration for prepareForSegue

Method signature is changed in swift 3.0

Replace this

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

With

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


Related Topics



Leave a reply



Submit