Search code examples
iphoneobjective-cuitabbarcontrolleruisplitviewcontrolleruitabbar

how to add tab icons and titles to the tab items in split view


I'm implementing a split view based application.

I have 3 tabs for 3 root views in my application at the bottom of the left view / root views. For this I added three view controllers to the tabbar in app delegete.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    RootViewController *tab1 = [[RootViewController alloc] init];
    DashboardViewController *tab2=[[DashboardViewController alloc] initWithNibName:@"DashboardViewController" bundle:nil];
    SendUpdatesViewController *tab3=[[SendUpdatesViewController alloc] initWithNibName:@"SendUpdatesViewController" bundle:nil];


    NSArray *tabViewArray=[[NSArray alloc] initWithObjects:tab1,tab2,tab3,tabBar,  nil];
    tabBar=[[UITabBarController alloc] init];
    [tabBar setViewControllers:tabViewArray];

    self.splitViewController.viewControllers = [NSArray arrayWithObjects:tabBar,_detailViewController, nil];

    self.window.rootViewController = self.splitViewController;


    [self.window makeKeyAndVisible];
    return YES;
}

Now I need to add titles icons and the corresponding actions for these tabs.


Solution

  • You can assign titles to your viewcontrollers using the title-property. By default these titles are shown in the tabbar. You can also customize the UITabbarItem for each controller. Read more about that here: http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UITabBarItem_Class/Reference/Reference.html#//apple_ref/occ/cl/UITabBarItem

    A good solution for this would be to overwrite the getters inside your viewcontrollers for the "title" and "tabBarItem" properties, making use of lazy instantiation to make sure the properties are properly set when they are accessed for the first time. This way you also keep this code contained in each viewcontroller's implementation.