Display XIB before UITabBarController?

2019-03-06 11:29发布

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.

3条回答
Viruses.
2楼-- · 2019-03-06 12:14

As far as I know, UITabBarController can not to be embedded in another viewController. With that in mind, you have these choices:

  1. Present a modal view (that you don't want)

  2. Hide tabBar on launch, and once credentials are verified, show the tabBar. But there is a downside: hiding/showing tabBar can not be animated.

  3. You can initiate your UITabBarController with only one viewController - the one that is going to ask for credentials, and once verified, add more viewControllers to the UITabBarController (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.

查看更多
Deceive 欺骗
3楼-- · 2019-03-06 12:22

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
}
查看更多
放我归山
4楼-- · 2019-03-06 12:25

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.

查看更多
登录 后发表回答