我的故事板
如果用户未登录,则rootViewController为Login
用户登录完成后,rootViewController是MainTabBarController
我已经做到了
但是,我遇到的问题是Logout
我的Logout
是dismissViewController
如果我的rootViewController是Login
,那么它可以工作
它将删除当前的ViewController,因此出现Login
但是当我的rootViewController是MainTabBarController
时,dismiss不起作用,我尝试徒然使用poptoRootViewController
。
我应该在Logout
中做什么?
我想这样
dismissController(true,{
rootViewController = `Login`
})
最佳答案
对于注销,请执行以下操作:-(在IBAction或调用注销的didSelect ..etc方法中添加以下代码)
// Making Login as rootViewController as user is no longer logged in
NSUserDefaults.standardUserDefaults().setBool(false, forKey: "isUserLoggedIn")
NSUserDefaults.standardUserDefaults().synchronize()
let loginVC = self.storyboard?.instantiateViewControllerWithIdentifier("Login") as! loginViewController
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDel.window?.rootViewController = loginVC
还可以在AppDelegate中添加以下内容:
// Checking user login status, if user already logged in then making main tab bar view controller as root view controller
let userLoginStatus = NSUserDefaults.standardUserDefaults().boolForKey("isUserLoggedIn")
if(userLoginStatus)
{
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let centerVC = mainStoryBoard.instantiateViewControllerWithIdentifier("MainTabBar") as! ViewController
window!.rootViewController = centerVC
window!.makeKeyAndVisible()
}
还有在验证用户凭据后完成登录验证的位置:-
@IBAction func loginTapped(sender: AnyObject) {
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let centerVC = mainStoryBoard.instantiateViewControllerWithIdentifier("MainTabBar") as! ViewController
// Important to set status to true
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "isUserLoggedIn")
NSUserDefaults.standardUserDefaults().synchronize()
appDel.window!.rootViewController = centerVC
appDel.window!.makeKeyAndVisible()
}
注:-不要忘记为所需的View Controller添加STORYBOARD ID来实例化它们
关于ios - iOS在dismissController时如何更改rootViewController,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37782001/