Sign up ×

I'm using iOS 5 with Storyboard. My UITabBar is created using the Interface Builder. I have two similar items in my TabBar that is the same list, just with different type of item in it. What I've done, but looks weird to me, is setting a different "Tag" to each UITableView and in the viewDidLoad, and then assign the right type according to the Tag.

- (void)viewDidLoad
    [super viewDidLoad];

    if (self.tableView.tag == 1)
        type = @"lent";
    else if (self.tableView.tag == 2)
        type = @"borrowed";

Any better way to do it? I'm not creating my UITabBar in code, so my AppDelegate is pretty empty! The type I set is just an attribute in one of my Core Data Entity, on a list I have Borrowed Items and on the other I have Lent Items, but they're the same entity.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

You could expose the type as a property on your common view controller, and set it when the relevant tab bar item is selected (tabBarController:didSelectViewController: from the UITabBarControllerDelegate protocol - your app delegate would be the tab bar controller delegate).

You would set this up as follows. Declare that your app delegate conforms to the UITabBarControllerDelegate protocol, then set it as the tab bar controller's delegate (you have to do this in code since the app delegate is not available to connect to in the storyboard). In your applicationDidFinishLaunching, add the following before you return YES:

UITabBarController *tbc = (UITabBarController*)self.window.rootViewController;
tbc.delegate = self;

Then implement the following delegate method:


    switch (tabBarController.tabBar.selectedItem.tag)
        case 1:
   = @"propertyA";
        case 2:
   = @"propertyB";

    NSLog(@"view controller is %@",viewController);


You'll need to cast the viewController variable to your actual view controller class, and also assign the relevant tags to the tab bar item of each view controller.

share|improve this answer
Hm, solution looks nice! So I simply put tabBarController:didSelectViewController: in my App delegate? Will give this a shot tomorrow morning and will keep you posted. Thanks! –  allaire Jan 20 '12 at 3:57
You'll also need to set the app delegate to be the tab bar controller's delegate, and declare that your app delegate conforms to the protocol. –  jrturton Jan 20 '12 at 7:19
Do you have a minute to share a little snippet of code, what I tried didn't work :/ –  allaire Jan 20 '12 at 14:16
I will in a few hours time :). In the meantime, you could update your question with the code you have tried, someone else might be able to help. It will also be useful for me to see what you have so far. –  jrturton Jan 20 '12 at 15:19
Updated my answer with example code. –  jrturton Jan 21 '12 at 22:48

What you have there should work fine. Another option would be to have a common UIViewController super class that has all the functionality and then subclass that base class and provide an implementation of viewDidLoad that sets the appropriate type. Then in Interface Builder you can set the UITabBar view controllers as your appropriate subtypes.

The result would be the same, but it may be a bit more clear in IB what is going on because you don't have to rely on remembering the meaning of each numerical tag.

share|improve this answer
Thanks for the suggestion :) –  allaire Jan 20 '12 at 3:56

Your Answer


