-3

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.

user19933
  • 1
  • 1
  • Please show us the relevant code. – ndmeiri Jun 25 '15 at 01:17
  • 1
    If you have specific code that isn't working as expected, post that code. – Duncan C Jun 25 '15 at 01:17
  • Sorry, I added the code. – user19933 Jun 25 '15 at 01:50
  • You said: "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." Can you please clarify the last part of this statement? – rb612 Jun 25 '15 at 01:57
  • When I print id in prepare for segue, it gives me the numeric string I'm trying to pass to the next view, but when I print it in the next view's viewDidLoad, I get an error because it is nil. – user19933 Jun 25 '15 at 04:17

1 Answers1

0

You were right your variables are not yet initialized, try this:

class signUpViewController: UIViewController {
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "toSignUp" {
        if let destinationVC = segue.destinationViewController as? signUpViewController {
            //pass the values to the strings that are initialized with the object
            destinationVC._firstNameField = self.firstName
            destinationVC._lastNameField = self.lastName
            destinationVC._emailField = self.email
            destinationVC.facebookID = self.facebookID
        }
    }
}

class signUpViewController: UIViewController {
    @IBOutlet weak var firstNameField: UITextField!
    @IBOutlet weak var lastNameField: UITextField!
    @IBOutlet weak var emailField: UITextField!
    //Create and initialize strings
    var _sfirstNameField = String()
    var _slastNameField = String()
    var _semailField = String()
    var facebookID = String()
    viewDidLoad() {
        //pass the values from the strings to the now initialized UITextField
        firstNameField.text = _firstNameField
        lastNameField.text = _lastNameField
        emailField.text = _emailField
        print(facebookID)
    }    
}
Icaro
  • 14,585
  • 6
  • 60
  • 75