简体   繁体   中英

Add UITabBarController NOT from the appDelegate?

Is it just me or literally ALL the examples that I have seen on the web for uitabbarcontroller require modifying the appDelegate? What if the tabbar comes later on in the app and there are some other screens before? Can someone explain this or point to an example where the first screen of the app is NOT a tabbar view? I am getting nuts from this and cant seem to be able to have the tabbar to work if not from the AppDelegate.

Thanks!!!

UPDATE:

So I am doing this in one of my view controllers in a method that triggers once you click on a button to move to the next screen (which is the tab bar view). It crashes during the execution:

  self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[FirstView alloc] initWithNibName:@"FirstView" bundle:nil];
UIViewController *viewController2 = [[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil]; //CRASHES HERE
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];

Any idea why? Thanks!!

It crashes in this line:

self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil]; //CRASHES HERE

You can push the UITabBarController to the navigation controller from any view controller. Doing so may collapse the navigation bars as the view controllers in UITabBarController may themselves contain UINavigationController .

In order to overcome this navigation bar issue, you need to hide the navigation bar of the current navigation controller from the view controller where you push the tab bar controller. Hide the navigation bar in viewWillDisappear: method of the current view controller.

- (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

Ok here we go. Suppose mainWindow.xib has one UINavigationController and one TabBarController. First few screens are deisgned with navigation controller later at some point when you want tabBarController what you need to do is just remove the navigationController's view from mainWindow and add tabBarController's view as a subview to window. I hope you understand if not let me know.

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.

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITabBarController_Class/Reference/Reference.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM