简体   繁体   中英

Show a login screen before a Tab Bar Controller

I'm doing an Twitter app, and on the AppDelegate -didFinishLaunchingWithOptions I'm using next code for loading the login view if NSUserDefaults on that object are empty:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

NSString *test = [prefs objectForKey:@"username"];
if (test == @"(null)" || test == nil) {
    LoginScreenViewController *login = [[LoginScreenViewController alloc] initWithNibName:@"LoginScreenViewController" bundle:nil];
    [self.window addSubview:login.view];
    [self.window makeKeyAndVisible];

} else {
    [self.window addSubview:tabBarController.view];
    [self.window makeKeyAndVisible];
}

And I'm able to load the login view, but then how would I dismiss it? The login screen has to show up before the tabBarController is loaded, so when the Login controller is done, the tab bar controller gets started as if it wouldn't have any other view before. Thanks in advance!

You could load your tab bar the same if the user is logged in or not (the second part of your if statement)

If the user is not logged in you could show your LoginScreenViewController as a modal view controller. This would sit above the tab bar controller.

Something like

[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];

NSString *test = [prefs objectForKey:@"username"];
if (test == @"(null)" || test == nil) {
    LoginScreenViewController *login = [[LoginScreenViewController alloc] initWithNibName:@"LoginScreenViewController" bundle:nil];
    [tabBarController presentModalViewController:login animated:YES];
}

I'm not sure if this will solve your problem, but don't use == for string comparisons. Use the compare: method of the NSString class.

if ([myNSStringObject compare:anotherNSStringObject] == NSOrderedSame) {
   //proceed with processing based on resultant matched strings
}
else {
   //proceed with processing based on resultant non-matched strings
}

Not sure if this will make a difference, but as your program becomes more complex, you might run into trouble not doing it this way.

When login completes call:

[login.view removeFromSuperview];
[self.window addSubview:tabBarController.view];

There should be only one UIView that belongs to a UIViewController added to a window, but if you remove that view you can add another.

you can present loginview as model popup. this is what i am doing.

      loginView = [[[LoginViewController alloc] initWithNibName:@"LoginView" bundle:nil] autorelease];
 UINavigationController* nav = (UINavigationController*)[tabBarController.viewControllers objectAtIndex:0]; 
loginView.navC = nav; [nav presentModalViewController:loginView animated:YES];

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