开发者

Having trouble with UITabBarControllers in Interface Builder

开发者 https://www.devze.com 2022-12-25 03:01 出处:网络
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

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 modify开发者_开发知识库ing 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 http://www.shelltoesmusic.com/files/tabbar_ib.png


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消