I am stuck on passing data from one ViewController to another. The scenario is as: I have 2 ViewControllers named : SearchDomainController and LoginViewController. What i am trying to do is pass the string value from SearchDomainController to the UITExtfield in LoginViewController.
In LoginViewController i have declared IBOutlet UITextField *domainField;
and also a property @property(nonat开发者_JAVA百科omic,retain) UITextField *domainField
.
The problem is when i create a new instance of LoginViewController in SearchDomainController and try _loginViewController.domainField.text = @"Some text";
the text never changes in UItextField on LoginViewController.
What did i miss ? And what are the best solution for this kind of problem? Thanx
My guess would be that _loginViewController.domainField
is nil at that time, which is probably because the view hasn't loaded yet, and the label is created when the view loads (via a nib) and not as soon as the view controller object is created.
In order to not depend on having the view fully loaded when passing the value, I would have used a separate property for passing along the title, i.e. _loginViewController.domainFieldText = @"Some text";
. Then in viewDidLoad
of the _loginViewController
, assign the value of domaonFieldText
to the actual label.
Alternatively, make sure the UILabel instance is created and not nil when you set its text from the other view controller.
Set the string as a property
of the AppDelegate in one view controller, and you can retrieve it from another, and set it as textField.text
精彩评论