BMN
BMN

Reputation: 8508

Access TabBarController variables in sub-ViewController

Basically, I have a TabBarController and some sub-views attached to this controller. The TabBarController has some properties that I would like to access in the sub-views.

Here's what I have :

MyTabBarController's .m

-(void)setDetails:(id)sender
{
    self.myVariable = @"This is a test";
    NSLog(@"Here I set my variable");
}

- (void)viewDidLoad
{
    NSLog(@"[LOAD] My Tab Bar Controller");
    [self setDetails:nil];
}

First subView's .m

- (void)viewDidLoad
{
    NSLog(@"[LOAD] FirstViewController");

    MyTabBarController *myTBC = (MyTabBarController *)self.tabBarController;
    self.headerName.text = myTBC.myViariable; // Here I just set the UILabel's text
    NSLog(@"Header name = %@", self.headerName);
}

Here's what I have in the logs :

2012-08-07 11:43:23.001 MyFirstproject[23632:15203] [LOAD] My Tab Bar Controller
2012-08-07 11:43:23.012 MyFirstproject[23632:15203] [LOAD] FirstViewController
2012-08-07 11:43:23.072 MyFirstproject[23751:15203] Header name = (null)
2012-08-07 11:43:23.116 MyFirstproject[23751:15203] Here I set my variable

My question is : As I can see in the logs, the viewDidLoad function of MyTabBarController is called before the FirstViewController's one. However, it seems the function setDetails of MyTabBarController is called after the function viewDidLoad of the FirstViewController.

How can this be possible ? Is there something I'm doing wrong here ?

Thanks !

Upvotes: 0

Views: 1616

Answers (1)

The iOSDev
The iOSDev

Reputation: 5267

Try to put the code for firstViewController in viewDidAppear method instead of viewDidLoad

Happy Coding :)

And for the difference bet'n those two just check it out the documentation regarding it on Apple's developer site :)

Formally viewDidLoad call's once at the time of loading the view

And viewDidAppearcall's when ever the view is about to appeared on the screen :)

Happy Coding :)

Upvotes: 1

Related Questions