登录后切换视图控制器..最佳实践 [英] Switching View Controller After Login..Best Practise
问题描述
我已经成功设置了我的应用程序流逻辑,除了登录之外,一切似乎都运行良好.我想让用户保持登录状态,以便在重新打开应用程序时首先显示个人资料屏幕.现在我有代码..
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.
推荐答案
在这种流程中我通常会玩window的rootViewController.例如
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];
}
然后当登录成功或注销时,您可以在任一控制器之间切换.通过使用 appdelegate 引用调用这些方法.
Then When Login Successful or Logout, you can switch between the Either Controllers. By calling these method with appdelegate reference.
这篇关于登录后切换视图控制器..最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!