Adding login screen in front of Cocoa Touch Tab Bar Application for IOS

≡放荡痞女 提交于 2019-11-27 19:10:01

In your AppDelegate, at the end of the application didFinishLaunchingWithOptions method you'll see this:

[window addSubview:tabcontroller.view];
[window makeKeyAndVisible];
return YES;

Simply initialize your login view controller and add it after the tabcontroller, like this:

initialScreenViewController = [[InitialScreenViewController alloc] init];
[window addSubview:tabcontroller.view];
[window addSubview:initialScreenViewController.view];
[window makeKeyAndVisible];
return YES;

In you login viewcontroller, after authenticating the user you can hide it like this:

[self.parentViewController.view setHidden:YES];

which allows you to show it again if you have a logout feature.

The standard way is the following:

  • Package everything related to login screen into a view and a UIViewController subclass which manages that.
  • Present that view modally in the app delegate in application:didFinishLaunchingWithOptions: by calling

    LoginController*loginController= ... ; // create the view controller for the login screen
    [self.tabController presentModalViewController:loginController animated:YES];
    

This way, the animation between the transition etc. is automatically handled.

You can later dismiss it after you successfully logs in. It can be done from inside the LoginController by

[self.parentViewController dismissModalViewControllerAnimated:YES];

However, I often need to do additional setup once the logging-in is done. So, I would first tell the app delegate that the login is done, and then perform

[self.tabController dismissModalViewControllerAnimated:YES];

from the app delegate. Then I can perform additional tasks there.

To communicate back to the app delegate, I would use NSNotification, but that might be a bit difficult for you.

One way which might be easier to understand (but uglier to my taste) is to define a method, say loginDone in the app delegate. Then, inside the LoginViewController, you can do

MyAppDelegate*appDelegate=[[UIApplication sharedApplication] delegate];
[appDelegate loginDone];

If you are starting with the default tab bar application you can do it like this:

  • In the MainWindow.xib, create a UIView that contains all the stuff you want to have on your password screen
  • Hook whatever you need up to IBOutlets in the AppDelegate, and write the method that checks if the password is valid.
  • In the applicationDidFinishLaunching method, replace [window addSubview:tabBarController.view]; with [window addSubview:/*whatever you called the view with the password stuff in it*/];
  • If the user enters the correct password do this:

[passView removeFromSuperview]; [window addSubview:tabBarController.view];

And you should be in the regular tab bar application.

I prefer to do the following:

In App Delegate's didFinishLaunchingWithOptions:

FirstViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
SecondViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
ThirdViewController *viewController3 = [[ThirdViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
UINavigationController *navController3 = [[UINavigationController alloc] initWithRootViewController:viewController3];

self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[navController1, navController2, navController3];

LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
UINavigationController *loginNavController = [[UINavigationController alloc] initWithRootViewController:loginViewController];

self.window.rootViewController = loginNavController;

Then after getting an authentication callback, you can have something like this in your App Delegate:

- (void)setAuthenticatedState:(BOOL)authenticated
{
    if (authenticated == YES) {
        dispatch_async(dispatch_get_main_queue(), ^(){
            self.window.rootViewController = self.tabBarController;
        });
    }else{
        // Stuff
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!