views:

24

answers:

1

I have the relatively common setup of a TabBarController whose tabs contain NavigationControllers which have TableViewControllers as their roots. I'm trying to perform some logic on initialization of one of these TableViewControllers but can't seem to find what init function gets called.

My goal is to add a listener in the TableViewController (that I have subclassed) which can respond to events by updating the navigationController.tabBarItem.badgeVluew property.

I've tried putting code into initWithStyle: as well as init but neither of them end up getting called. I've also tried putting it in viewDidLoad, but that only gets called once the controller actually appears (I need to have it happen as soon as the controller is loaded / as soon as the tab bar item shows up).

Does anyone know where I would put this code for it to happen on initialization of the controller?

Also, this is all set up through interface builder / NIBs. I'm not adding the nav controller or tableviewcontroller manually, which is why it's not clear what init function I need to override.

+1  A: 

If you select one of your UITabBarItems in IB, you will see 'View loaded from "YourView"'. Click into this "gray" View. In the Inspector window you will see in the Attributes Tab (the tab on the left) the title and the NIB name which will be loaded (lets call it "YourNibName").

Now select the right tab of the inspector (Identity) and change the Classname (Combo next to Class) to your "YourViewController" class, which you must create in xcode. Don't use the standard ViewController, which is already selected. The InterfaceBuilder loads your nib and attaches it to your ViewController.

Open YourNibName and change FilesOwner's Class (Inspector, right Tab) to "YourViewController", too.

Your TabBar's NIB contains a FilesOwner, too. Create a ViewController for this FilesOwner and set its Class to this Controller (i.e. TabBarController)

In "TabBarController" you can find out which Tab was selected by using this code:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{

if ([viewController.nibName isEqualToString:@"NIBName1"]){

    // Do something here, if you like. (i.e. Save the state in a string or int)
}

if ([viewController.nibName isEqualToString:@"NIBNAme2"]){

    // Do something here, if you like. (i.e. Save the state in a string or int)
}

...
}

Here you can do something "global" or preinitialize something. This is ONE thing you can do.

INIT OF YOUR VIEWS:

If you select a Tab and the view (which is handled by YourViewController) will be shown for the first time, "viewDidLoad" will be called in "YourViewController"

- (void)viewDidLoad {

// Here you can add views programatically
[self.view addSubview:myNavigationController.view];
[self.view bringSubviewToFront:myNavigationController.view];

    // And if you like, do some INIT here


[super viewDidLoad];

}

I hope this is what your question was about.

JackPearse
@jackpearse I already have the controller loaded by the view/tabbarcontroller connected with the proper UITableViewController subclass, but thanks for the explanation. The real problem is that I need to run logic *before* the controller is ever selected (basically as soon as its initialization function is called). tabBarController:didSelectViewController: and viewDidLoad are called only when you first go to that tab. I want to put logic in my viewcontroller subclass that gets executed when it's created, even before it's shown.
Chris R