Separate XIB for UITabBarController?

I want to add a UITabBarController view to my window, after it has been running for a while (when a user pushes a button in my game). Adding a UITabBarController to the same xib as the window dwells in is easy. The thing is that I don't want an instance of the tab bar until the user pushes the button (mostly because I don't know what data the tab bar controllers should have until the user pushes that button).

So I thought that maybe I could use a separate xib for the UITabBarController (preferably I want to use xib instead of code), that I can instantiate in code when the time comes. This proved to be a lot harder than I thought. I've tried making the File Owner of the XIB to a UITabBarController, and then add a tabbar and controllers, but it doesn't look like Xcode puts these things together automatically as when you add a UITabBarController explicitly from the library. So then I tried making the file owner to an UIViewController instead, add a UITabBarController, and then add that controller's view to the UIViewController's view. This kinda worked. It is easy to customize (which is why I want to use XIBs), but unfortunately, the tab bar's buttons don't work to 100%. Only the upper 50% of the buttons are tappable. Beneath, nothing happens if you tap, which isn't acceptable. Is this a bug? Or are you not allowed to add UITabBarControllers to subviews of window?

Any ideas how I can achieve what I want, preferably using XIBs?

You can see the "bug" here:
http://dl.getdropbox.com/u/608462/TabBarTest.zip

Posted on Oct 7, 2009 10:16 AM

Reply
3 replies

Oct 7, 2009 7:13 PM in response to rawgrarr

rawgrarr wrote:
... are you not allowed to add UITabBarControllers to subviews of window?

Correct. The tab controller's view must be the root view. From The Views of a Tab Bar Controller in the +UITabBarController Class Reference+:
Because the UITabBarController class inherits from the UIViewController class, tab bar controllers have their own view that is accessible through the view property. When deploying a tab bar interface, you must install this view as the root of your window. Unlike other view controllers, a tab bar interface should never be installed as a child of another view controller.

Any ideas how I can achieve what I want, preferably using XIBs?

Yes. Please take another look at the 3 workarounds listed here (same link I posted in your previous thread on this topic): [http://discussions.apple.com/thread.jspa?messageID=10002001&#10002001].

From what you've said so far, I think you might want no. 1 from that list. To implement that solution, start with the Tab Bar app template (or any project with a similar MainWindow.xib). Next make a controller subclass and a matching xib (UserInterface->View XIB template) for your starting screen (don't forget to set File's Owner to your subclass, then connect its view). Then, in the @implementation of the app controller, code something like this:

// MyProjAppDelegate.m
#import "MyProjAppDelegate.h"
#import "StartingViewController.h"
@implementation MyProjAppDelegate
@synthesize window;
@synthesize tabBarController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIViewController *firstVC = [tabBarController.viewControllers
objectAtIndex:0];
StartingViewController *modalVC = [[StartingViewController alloc]
initWithNibName:@"StartingView" bundle:nil];
NSLog(@"appDidFinishLaunching: firstVC=%@ modalVC=%@", firstVC, modalVC);

// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];
[firstVC presentModalViewController:modalVC animated:NO];
[modalVC release];
}

Finally, this method, added to the starting controller @implementation, brings up the first tab bar screen when the user is done with the starting screen:

// StartingViewController.m
#import "StartingViewController.h"
@implementation StartingViewController
- (IBAction)done {
[self.parentViewController dismissModalViewControllerAnimated:YES];
}

Things get slightly more complicated if the user can navigate to other starting views before going to the tab bar screen, so let us know if you have any problems with that control structure.

Hope that helps!
- Ray

Oct 8, 2009 4:39 PM in response to rawgrarr

rawgrarr wrote:
Looking at this makes me think using code only is much more simple. Interface builder is supposed to make things easier, but in this case, I think it's the other way around.

Well in this case the code is required, since there's no way to specify a modal controller in IB. Also remember, my example was specifically intended to work around the structure defined in IB.

The tab bar controller is at the top level in MainWindow, which normally means that controller's view will be the root view (though the root is actually determined in code when the view is added directly to the key window). We might have made MainWindow.xib more "self documenting" by defining the StartingViewController object there, but that might only add confusion because there's no way to show the modal relationship. The tab bar controller is indeed the root controller, but it's view isn't going to appear first. AFAIK, there's no way to either define or document that funtionality in IB.

I wouldn't generalize from this particular case, though. In fact if we start with the Tab Bar app template, the xib's are still doing most of the heavy lifting.

That said, I often find myself favoring a code solution in this forum, not because it's easier to build, but because it's easier to describe. Basically, an IB solution puts me on the wrong end of "A picture is worth a thousand words.", so my post can turn into a 38-step, hand written tutorial instead of a simple cut and paste. This is especially true if I"m not certain of the developer's skill level and/or English. Code is an international language, as are graphical images. But an English description of a graphic is anything but international.

Hope the above is relevant to your question! Please let us know what solution you choose. If you like the modal view solution, please also let us know how it works out for you.

\- Ray

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Separate XIB for UITabBarController?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.