开发者

Showing a login screen as a modal view

I'm developing an app that can be used without logging in immediately, but certain tabs require login. So I want to have my login view slide up whenever those tabs are opened. If the user hits the Cancel button in the login view, the modal window should be dismissed and it should return to the same view the user was at before. If the login is successful, the window should be dismissed and the tab should load. What's the best way to implement this? Should I make a custom tab bar controller?

EDIT: I took Caffeine's approach. This seems to be working for me:

// App delegate

#pragma mark - UITabBarController delegate

- (BOOL)tabBarController:(UITabBarController *)tabBarController
          shouldSelectViewController:(UIViewController *)viewController {
  if ([[viewController topViewController] loginRequired]) {
    LoginViewController *logi开发者_如何转开发nViewController = [[LoginViewController alloc] init];
    UINavigationController *navController = [[UINavigationController alloc]
      initWithRootViewController:loginViewController];
    [tabBarController presentModalViewController:navController animated:YES];
    [loginViewController release];
    [navController release];
    return NO;
  } else {
    return YES;
  }
}


You can try implementing the tabBarController:shouldSelectViewController of UITabBarControllerDelegate. Present the modal login controller from within that delegate method and if it's successful return YES, otherwise NO.


  1. Make your tabBarController your rootViewController.

  2. Create class and view for your LoginController.

  3. In the first tab that would launch, (or the tab that you would require a modal view) go to the viewDidLoad method and reference LoginViewController and use

     LoginController *lvc = [[LoginController alloc]initWithNibName:@"LoginController" bundle:[NSBundle mainBundle]];
    
         UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:LoginController];
    
    
     [self presentModalViewController:navController animated:NO];
    
      // This gives navigation control to the login controller //
    
  4. If login is successful, you can just do [self dismissModalViewControllerAnimated:YES];

  5. If login is not successful, you can navigate go to a ForgotLoginController for password recovery or just popToRootViewController and go back to the previous screen or pop to the first view in the navigation stack and stay on the Login options.


You could simply use TabBarController delegate methods to detect tab changes, and then storing the selected index for later use.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜