I'm facing this problem for a couple of months and i don't know what is the best solution to solve it.The problem is,i need to load a XIB before my UITabBar shows up,more clearly,i have my first view which is to the user login(NO TABBAR SHOULD BE DISPLAYED),when user login,the app verify the information and after should load the view with a UITabBarController,but every time i try do that without presenting the login view modally,both of the views are displayed,the login view and the tabbar view.
问题:
回答1:
You could set first the loginViewController as rootViewController
of your main window
, then after the user is logged in, set the tabBarController as rootViewController
.
Something like this (assume your loginViewController is viewController1
):
Appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
UINavigationController *myNav1=[[UINavigationController alloc] initWithRootViewController:viewController1];
UINavigationController *myNav2=[[UINavigationController alloc] initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:myNav1,myNav2, nil];
//set the login view
self.window.rootViewController = viewController1;
[self.window makeKeyAndVisible];
return YES;
}
-(void)setTabBar{
//self.viewController1=nil;
self.window.rootViewController = self.tabBarController;
}
Then from the loginViewController call the method setTabBar
of the appDelegate.
LoginViewController.m
#import "AppDelegate.h"
-(void)loginOK{
AppDelegate *del=(AppDelegate*)[[UIApplication sharedApplication] delegate];
[del setTabBar];
//you could add some animation transition between views
}
回答2:
As far as I know, UITabBarController
can not to be embedded in another viewController
. With that in mind, you have these choices:
Present a modal view (that you don't want)
Hide
tabBar
on launch, and once credentials are verified, show thetabBar
. But there is a downside: hiding/showingtabBar
can not be animated.You can initiate your
UITabBarController
with only oneviewController
- the one that is going to ask for credentials, and once verified, add moreviewControllers
to theUITabBarController
(that will add more tabs). This is also the kind of behavior you can see in some app, e.g. Bank of America (http://itunes.apple.com/us/app/bank-america-mobile-banking/id284847138?mt=8)
There might be better practices. These are my suggestions.
回答3:
As an easy way, add your view as a subview of your window, and dismiss it when you don't need it anymore.
For example, put this code in your appdelegate (assuming loginController
is a property of your appdelegate...there are other ways, this is just an example):
[self.window addSubview:self.loginController.view];
When you want to dismiss the view, remove it:
[self.loginController.view removeFromSuperview];
Don't forget to properly release loginController
.
This way your view is just simply "overlayed" over your tabbar views. There are other answers here that effectively only swap the tabbar view into your view hierarchy after your login is complete, if that's what you want.