views:

251

answers:

2

I'm new to iphone development.

I've read tutorials about implementing a tab bar controller on the window (eg as the main controller for the app). But how can I create a tab bar controller as a 'standalone' UIViewController that can be called by a navigation bar controller?

Basically my navigation bar controller has an array of UIViewControllers that it displays in the table and then loads the appropriate view/controller when a user selects an item.

Now I want one of these loaded views/controllers to be a tab bar controller. How can I do this?

I'm not sure how to create a tab bar controller on its own without having an outlet/instance in the application delegate.

Hope that made sense. Thanks.

A: 

You have two options.

First. Create UITabBarController (general alloc-init stuff) and push it to UINavigationController.

Second. You can create custom UIViewController and place UITabBar there. After that you can customize it and push that custom UIViewController to navigation controller. Code will look like this:

UITabBar *tabBar = [[UITabBar alloc] initWithFrame:
    CGRectMake(0, self.view.frame.size.height - 49, 320, 49)];
tabBar.autoresizingMask = 
    UIViewAutoresizingFlexibleTopMargin | 
    UIViewAutoresizingFlexibleWidth;
[self.view addSubview:tabBar];

You can use similar code in viewDidLoad method of UIViewController, for example. To process tab change event, you'll have to implement UITabBarDelegate protocol and assign it (for example to UIViewController itself):

tabBar.delegate = self;

After that you'll have to implement

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item

method that'll help you to catch events.

Alexander Babaev
Thanks. So there's no way to use the tabbarcontroller as is in IB? I want all the features of a tabbarcontroller but instead I want to be able to display it in my own view instead of on the window.
Nebs
+2  A: 

Although previous answer was is correct, I'd just like to point out that Apple won't be happy with this:

You never want to push a tab bar controller onto the navigation stack of a navigation controller. Doing so creates an unusual situation whereby the tab bar appears only while a specific view controller is at the top of the navigation stack. Tab bars are designed to be persistent, and so this transient approach can be confusing to users.

Quotation from: Apple View Controller Programming Guide

Read Human Interface Guidelines, your app might be rejected for "breaking the interface rules". What's more, you'll also have to handle all the vieWillAppear/Disappear etc manually. I'm pretty sure there's another way of designing the interface.

Regards, Paul

Pawel