How to pass data (string) from a class to the Viewcontroller TextField

1

I want to pass data from a class of type NSObject to be shown in my TextField Viewcontroller . I tried to use the prepareForSegue function but could not.

Someone knows how to pass the data, even if it is using the prepareForSegue function or another way to pass the data.

    
asked by anonymous 30.05.2014 / 14:04

1 answer

4

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.

    
14.07.2014 / 15:12