I'm trying to pass a string between view controllers. When I pass a string to the text of a UIButton through prepareForSegue it works, but when I try to pass it to a string declared as "id: String!", it remains nil. I think it's cause the variable isn't initialized yet when I call prepareForSegue, but I'm not sure how to resolve it.
Sorry, here's my code:
class signUpViewController: UIViewController {
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toSignUp" {
if let destinationVC = segue.destinationViewController as? signUpViewController {
destinationVC.firstNameField.text = self.firstName
destinationVC.lastNameField.text = self.lastName
destinationVC.emailField.text = self.email
destinationVC.facebookID = self.facebookID
}
}
}
class signUpViewController: UIViewController {
@IBOutlet weak var firstNameField: UITextField!
@IBOutlet weak var lastNameField: UITextField!
@IBOutlet weak var emailField: UITextField!
var facebookID: String!
viewDidLoad() {
print(facebookID)
}
}
Still couldn't resolve this issue. I printed firstNameField.text in viewDidLoad and it was also nil, but the text field in the view had the string from the previous view and when I press the submit button and perform my submitForm function, the fields pass the desired strings. I temporarily solved this by storing it in NSUserDefaults, but I'm still curious about this.