Launching a login view before the tab bar controll

2019-03-16 13:54发布

I have an ios5 app developed using storyboards that currently displays a tab bar controller view on initial launch. I would like to display a login screen before the tab bar controller is displayed. The user would enter his username & password, the system would then authenticate the user and then if successful, display the tab bar controller.

I have tried the following 3 options with no luck.. any ideas ?

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    // Option 1
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    PointsViewController *firstVC = [[tabBarController viewControllers] objectAtIndex:0];
    UIViewController *loginViewController = [[LoginViewController alloc] init];
    [firstVC.navigationController pushViewController:loginViewController animated:YES];

    // Option 2
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UIViewController *loginViewController = [[LoginViewController alloc] init];
    [tabBarController presentViewController:loginViewController animated:NO completion:nil];  

    // Option 3
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UIViewController *loginViewController = [[LoginViewController alloc] init];
    [tabBarController presentModalViewController:loginViewController animated:NO];

    return YES;
}

4条回答
甜甜的少女心
2楼-- · 2019-03-16 14:03
我只想做你的唯一
3楼-- · 2019-03-16 14:06

Finally figured this one out.. here is what you need to do:

  1. Add a standalone login view to the storyboard.

  2. Select the login view and in the attributes inspector, check the 'Is Initial View Controller'. This will switch the initial view being launched from the tab controller to the login view, thereby solving the whole issue of displaying the login screen first.

  3. Add a button to the login view and create a segue to load the tab controller on push of the button. (Or you can create a segue from the login view to the tab controller view and programmatically invoke the segue as necessary).

  4. Select the login view and choose option Editor > Embed In > Navigation Controller

  5. In the attributes inspector for the Navigation controller, uncheck the 'Shows Navigation Bar' option (this is a cosmetic change; I am assuming you don't need a navigation bar showing on the login screen !!)

That's it :)

查看更多
狗以群分
4楼-- · 2019-03-16 14:13

I met this problem just now and I have perfectly solved this by adding the following code, which you also didn't use.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    ...
    [self.window makeKeyAndVisible];
}
查看更多
Anthone
5楼-- · 2019-03-16 14:24

You can use a modal view. You can check if the user is logged in. If not then you can use a modal view to get the login information. You can create a UIViewController in the storyboard and then use the instantiateViewControllerWithIdentifier: method to create the login screen from the storyboard. Then simply show it modally.

查看更多
登录 后发表回答