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:
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
How to Implement Tableview Inside Tableview Cell in Swift 3
Swift - the Data Couldn't Be Read Because It Isn't in the Correct Format
Firebase Pod Install - Pod 'Firebase/Database' - Required a Higher Minimum Deployment Target
Nsdateformatter Datefromstring Always Returns Nil
Get User Swiping Direction in Uipageviewcontroller
How to Change Pagetabview Programmatically in iOS 14, Swiftui 2
iOS 9 Cloudkit: Query Does Not Return Anything While Connected to Cellular Network
Switching from Xcode3 to Xcode4 - Can't Load Programs Onto Older Ipod Touch
Xcode 7 Crash: [Nslocalizablestring Length] 30000
Play a Video from Youtube in a Avplayerviewcontroller in Swift
Uitableviewcell Textlabel Color Not Changing
Convert Nsattributedstring into Data for Storage
Multiple Uicollectionview in One Controller
How to Dynamically Add Rows to a Specific Uitableview Section
Ios9: Alternative to Uidevice.Currentdevice().Setvalue(...) to Force Orientation
Launchd_Sim Crashing: Could Not Create Temporary State Directory