I need to stop a segue transition and show alert with a textField. But then, when I press "OK" button, I want to pass to the next view, the content of the textfield.
The problem is when I declare a view controller destination in prepareForSegue and I pass the text, because this is nil. I write my expample:
override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {
var shouldSegue:Bool = true
if identifier == "FirstToSecondView" {
var alert = UIAlertController(title: "End the First", message: "Are you sure you want to end the first?", preferredStyle: UIAlertControllerStyle.Alert)
alert.addTextFieldWithConfigurationHandler{ (textField) in
textField.placeHolder = "Name"
}
presentViewController(alert, animated: true, completion: nil)
var okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
UIAlertAction in
println("OK Pressed")
let tf = alert.textFields?.First as? UITextField
//name is a declared variable above
if tf != nil {self.name = tf.text}
self.performSegueWithIdentifier("WorkoutToSummary", sender: self)
}
var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
UIAlertAction in
println("Cancel Pressed")
shouldSegue = false
}
alert.addAction(okAction)
alert.addAction(cancelAction)
}
return shouldSegue
}
And then in the prepareForSegue, I put that:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
var namePass: String = self.name
if segue.identifier == "FirstToSecondView" {
let secondViewController = segue.destinationViewController as SecondViewController
secondViewController.name = namePass
}
}
But at the moment that prepareForSegue is launched, self.name is nil
Thanks in advance!!
Aucun commentaire:
Enregistrer un commentaire