简体   繁体   中英

Show login screen before tab-controller view

i have a tabBarController application and using .xib files for the interface not the storyboard i have this code by default in the appdelegate

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[PopAdsFirstViewController alloc] initWithNibName:@"PopAdsFirstViewController" bundle:nil];

UIViewController *viewController2 = [[PopAdsSecondViewController alloc] initWithNibName:@"PopAdsSecondViewController" bundle:nil];

self.tabBarController = [[UITabBarController alloc] init];

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

self.window.rootViewController = self.tabBarController;

[self.window makeKeyAndVisible];

return YES;

i have created a Login View and don't know how to show it before the tabBarView and hide t after a successful login.

One way would be to show it as a modalView on launch. Dismissing upon successfull login? eg:

UIViewController myLoginViewController = [[MyLoginViewController alloc] init withNibNamed:"MyLoginViewController"]; //Or whatever you instantiation is
[myTabViewController presentModalViewController:myLoginViewController animated:YES];

And to dismiss it (Hide it)

//This should be done from the original View Controller i.e. myTabViewController preferably in a delegate called by the modal view controller.
[self dismissModalViewControllerAnimated:YES];

Documentation on modalViewControllers: http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html

The way that I did it for one of my apps is to just add them in the correct order. Add your tabbar controller to your window, then add the login controller over the top of the tab bar. Then show your window. The user won't see anything but your login controller. Once you login, you can just remove the login controller from view.

This way is probably best if you have information you need to hide until login. The other way is to only launch the login view only. On successful login, remove the login and add the tab bar controller. Either way is fine.

Presenting modally is probably the easiest, but requires a view in place before presenting. So if the data and view under the login controller isn't that sensitive, you could consider this option.

Another way would be using LoginViewControllerDelegate in your appDelegate.h file

In your .h

    #import "yourLoginViewController"
   //and add LoginViewControllerDelegate

Then in your .m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    yourLoginViewController *loginView = [[yourLoginViewController alloc] initWithNibName:@"yourLoginViewController" bundle:nil];
    loginView.delegate = self;
    [window addSubview:loginView.view];
    [window makeKeyAndVisible];
}
//add this one
- (void)loginViewControllerDidFinish:(yourLoginViewController *)loginViewController {
    [window addSubview:tabBarController.view];
}

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