You can not set the text for the UITextField (or any other component such as UILabel or UITextView) in the prepareForSegue
method, because all components of the destination view controller are not yet allotted (all of them are now nil). They will only be allocated when the viewController is presented.
The right thing is to create a NSString property in the target viewController and then set the text of the textField with the created property.
This way:
No prepareForSegue
use the following:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqual:@"ViewController2"])
{
ViewController2 *vc2 = [segue destinationViewController];
vc2.myString = self.textField.text;
}
}
and within the target viewController in method viewDidLoad
:
- (void)viewDidLoad
{
[super viewDidLoad];
(...)
self.textField.text = self.myString;
}
I hope I have helped.