views:

409

answers:

1

I'm building an app in which the root view/window is a tab-based view (created using the XCode wizard for creating a tab-based iPhone app), but there is also a point in the app where I want to create another tab-based view and present it modally.

I was having so much trouble creating the modal tab-based view in IB that I eventually just did it in code, kinda like this:

// *** In the event handler that causes the second-tab view to be presented ***
MyTabViewController *tabVC = [[MyTabViewController alloc] init];
[self presentModalViewController:tabVC.tabBarController animated:YES];
[tabVC release];

// *** Inside init() definition in MyTabViewController.m ***

UIViewController *vc1 = [[MyViewController1 alloc] init];
UIViewController *vc2 = [[MyViewController2 alloc] init];

tabBarController_ = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
tabBarController_.viewControllers = [NSArray arrayWithObjects:vc1, vc2, nil];
tabBarController_.selectedIndex = 0;

This worked fine until I started trying to write to tabBarController_.tabBar.items to set the titles and images for the buttons, which it apparently doesn't want to let you do for a TabBar that is owned by a TabBarController, giving this error:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 
'Directly modifying a tab bar managed by a tab bar controller is not allowed.'

So I tried going back to implement MyTabViewController using Interface Builder so that I could set the buttons up in there, but I can't figure it out. These are the steps I took to get where I am:

  1. Created new class descending from UIViewController in XCode, and checked the "with XIB" option.
  2. Dragged a TabBarController into the XIB (the one with the yellow ball behind it).

The thing I can't figure out is how to get the TabBar to take over the view. Right now the View that comes with the XIB automatically is empty, and I have this UITabBarController that is totally disconnected from it. If I try to drag the TabBar from within the UITabBarController into the View, it appears to make a new TabBar instead of positioning my TabBar into the view so that it occupies the whole view.

I apologise if I haven't explained this very clearly, but I'm really struggling to understand the linkage between the TabBarController and the View, i.e. can't figure out how to get the TabBarController to actually display.

Any help with this would be much appreciated. I have attached a screengrab from IB if that helps at all.

alt text

+1  A: 

The tab bar controller picks up the labels and images for the tabs from the view controllers it manages. The label comes from the view controllers' title. The image comes from the view controllers' tabBarItem properties. You can set both of these up in the init method of MyViewController1 and MyViewController2.

- (id)init {
    if (self = [super initWithNibName:@"MyViewController" bundle:nil]) {
        self.title = @"My View Controller";

        UIImage* anImage = [UIImage imageNamed:@"MyViewControllerImage.png"];
        UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"Home" image:anImage tag:0];
        self.tabBarItem = theItem;
        [theItem release];
    }
    return self;
}

Every view controller has a view property. The tab bar controller is also a view controller so it too has a view property. Add the tab bar controller's view property to the view that is currently blank.

Example:

[view addSubview:tabBarController.view];

See documentation if you have questions. It's very complete.

Giao
Thanks very much for your help Giao. I can now see how to achieve this in code.One final question though - is it actually possible to achieve what I'm trying to achieve using Interface Builder? I read the Apple docs you linked, and specifically the first paragraph of the "Creating a Tab Bar Interface Using a Nib File" section seems to be hinting that you should never put tab bar controllers into any nib other than the main window nib for your app.Am I reading that right?
glenc
No, it's saying that it's more awkward to create tab bar controllers in the NIB file. This is probably due to the fact that adding the view that will contain the tab bar must be done in code. At that point it's just simpler to do everything in code.
Giao
OK thanks for your help Giao. I've done it all in code now and it seems to be working fine after I started accessing the tabBarItem property of the view controllers.
glenc