views:

24

answers:

1

How can I accomplish the following:

  1. When my app loads a UIView will show 4 buttons
  2. Clicking on a button will load a UITabBarController (not a UIView with a UITabBar) that can display multiple views.

This seems challenging to me, because in order for me to use the UITabBarController I need to add this to the window's subview in my appDelegate. By doing so, my app automatically will load with the UITabbarController in the root view.

+1  A: 

You don't need to add the UITabBarController in the application delegate, that's just the most common way to use it. You can have your initial view use a simple UIViewController, then when the button is pressed load the UITabBarController (either programmatically or from a nib), and then display it.

The following is an example of what might be in your app delegate:

- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    // viewController is a UIViewController loaded from MainWindow.xib with a button that calls loadTabBarController
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

    return YES;
}

- (IBAction) loadTabBarController {
    self.tabBarController = [[[UITabBarController alloc] initWithNibName:@"MyTabBarController" bundle:nil] autorelease];
    [viewController.view removeFromSuperview];
    [window addSubview:tabBarController.view];
}
Cory Kilger
how do i call loadTabBarController if I am in another UIViewController?
Sheehan Alam
In my example it would be wired up in Interface Builder. MainWindow.xib would load viewController. If you were to do it programmatically you could set the target of the button to be the app delegate and set the action like normal.
Cory Kilger
lets say viewController has a button that is connected to an IBAction in Interface Builder. How does viewController get a reference to window if it is in the appDelegate?
Sheehan Alam
The window is also in the nib, however in my example the view controller doesn't need to reference the window, just the application delegate.
Cory Kilger