I am calling a segue programatically, Can any one please help me how can pass parameters ?
@IBAction func update(sender: AnyObject) {
self.performSegueWithIdentifier("showUpdate", sender: nil)
}
I am calling a segue programatically, Can any one please help me how can pass parameters ?
@IBAction func update(sender: AnyObject) {
self.performSegueWithIdentifier("showUpdate", sender: nil)
}
Swift 4:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ExampleSegueIdentifier" {
if let destinationVC = segue.destination as? ExampleSegueVC {
destinationVC.exampleStringProperty = "Example"
}
}
}
Swift 3:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ExampleSegueIdentifier" {
if let destinationVC = segue.destinationViewController as? ExampleSegueVC {
destinationVC.exampleStringProperty = "Example"
}
}
}
The performSegueWithIdentifier method takes two arguments, 1. the segue identifier, 2. the parameter you are passing which is of type AnyObject?
@IBAction func update(sender: AnyObject) {
self.performSegueWithIdentifier("showUpdate", sender: sender)
}
Then in the prepareForSegue method, you check the segue identifier and cast the sender parameter to the type you passed in earlier.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showUpdate" {
guard let object = sender as? ObjectToUpdateType else { return }
let dvc = segue.destinationViewController as! DestinationViewController
dvc.objectToInject = object
}
}
Prepare for segue can pass data along.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "showUpdate") {
if let vc: DestinationVC = segue.destinationViewController as? DestinationVC {
vc.variable = variableToPass
}
}
}