I have a show-type segue. I want destinationViewController nameTextField
to contain some text in it when segue is performed. I tried doing:
override func prepare (for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ToEditScreen" {
let svc = segue.destination as! EditViewController
svc.nameTextField.text = cars[selectedIndex!].name //crashes
svc.nameString = cars[selectedIndex!].name
}
}
The error I get is: Unexpectedly found nil while implicitly unwrapping an Optional value.
I know that cars[selectedIndex!].name
is not nil.
If I delete the textfield line everything is fine and nameString
does get its value which is equal to cars[selectedIndex!].name
. I know I can do:
nameTextField.text = randomString
but it seems like an extra step. Can I populate textfield.text from original ViewController
?
You can add a string variable to the destination vc and set it there or
let svc = segue.destination as! EditViewController
svc.loadViewIfNeeded() // add this line
svc.nameTextField.text = cars[selectedIndex!].name