user2520938
user2520938

Reputation: 421

accessing navigation controller 'target''s properties in segue

I have a segue between a scene and a navigation controller. The navigation controller embeds a view which has a textfield which I want to initialize in the prepareForSegue method.

So it's:

Scene A -> Nav controller -> scene B

But, in Scene A.prepareForSegue, only Nav controller's properties are allocated, and all Scene B's properties are still nil. So if I do:

UINavigationController *cont=[segue destinationViewController];
AddToDoItemViewController *atodo=(AddToDoItemViewController*)cont.topViewController;
[atodo preparetextfield2];

It won't work, because the textfield in the todo is still set to nil. So how do I fix this?

And here is the preparetextfield2 method:

-(void)preparetextfield2
{
    self.textField.text=@"test";
}

Upvotes: 0

Views: 57

Answers (1)

Greg
Greg

Reputation: 25459

In your AddToDoItemViewController.h file create property to keep the value, for example:

@property(nonatomic, strong) NSString *myString;

And update your text field in viewDidLoad:

-(void)viewDidLoad{
    //...Your code
    self.textField.text = self.myString;
}

And in preparetextfield2 just update the string:

-(void)preparetextfield2 { 
    self.myString = @"test";
}

This happened because your controls hasn't been loaded in that stage when you call prepareForSegue so your text field is nil.

Upvotes: 1

Related Questions