Switching View Controller After Login..Best Practi

2019-09-06 00:59发布

问题:

I've successfully set up my app flow logic and everything seems to be working fine except the login. I would like to keep the users logged in so when the app is reopened the profile screen is the first to show. Right now I have code..

override func viewDidAppear(animated: Bool) {

    var currentUser = PFUser.currentUser()
    println(currentUser.objectId as String)

    if currentUser != nil {
        self.performSegueWithIdentifier("loginsuccess", sender: nil)
    } else {
        // Show the signup or login screen
    }
}

It works but it seems to be too slow and the login screen flashes for a quick second. Ive searched for a cleaner way to do this but haven't found much.

回答1:

In this kind of flow I usually play with the rootViewController of window. e.g

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    if ([UserDefaultsSingleTon IsUserLogin])
    {
        [self SetRootToHomeView];
    }
    else
    {
         [self SetRootToLoginView];
    }
    return YES;

}

-(void)SetRootToLoginView
{
   self.loginController = [[LoginController alloc] initWithNibName:@"LoginController" bundle:nil];
   self.window.rootViewController = self.loginController;
   [self.window makeKeyAndVisible];
}

-(void)SetRootToHomeView
{
   self.homeController = [[HomeController alloc] initWithNibName:@"HomeController" bundle:nil];
   self.window.rootViewController = self.homeController;
   [self.window makeKeyAndVisible];
}

Then When Login Successful or Logout, you can switch between the Either Controllers. By calling these method with appdelegate reference.