How to pass data(string) from a class to Viewcontroller’s Textfield

Asked

Viewed 516 times

1

I want to pass data of a class of the type NSObject to be shown in my TextField Viewcontroller. I tried to use the function prepareForSegue but I couldn’t.

Does anyone know how to pass the data, even if it is using the function prepareForSegue or another way to pass the data.

  • 1

    Is it possible to detail more or include some code? The question is a bit obscure.

  • Thank you for answering but I already found something similar to what I want [http://answall.com/questions/17472/usando-um-objeto-em-diferentes-metodos-no-objective-c] . What I want has to do with the MVC, and when I asked the question I couldn’t explain.

1 answer

4


You cannot set the text to Uitextfield (or any other component like Uilabel or Uitextview) in the method prepareForSegue, because all the components of the target view controller are not yet allocados (at this point all of them are nil). They will only be allocados when viewController is presented.

It is right to create a Nsstring Property in the target viewController and then set the text of textField with the created Property.

Thus:

In the 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 the method viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];

    (...)
    self.textField.text = self.myString;
}

I hope I’ve helped.

  • Thank you for answering, yes this is one of the solutions. Fortunately I finished the project a few weeks ago and the method I used to pass the data was Singleton.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.