作者:命硬D小童鞋 | 来源:互联网 | 2023-02-05 19:10
Ihave3ViewControllers:1)LoginViewController.2)MainViewController.3)LogoutViewController.我
I have 3 View Controllers: 1) LoginViewController. 2) MainViewController. 3) LogoutViewController.
我有3个视图控制器:1)LoginViewController。 2)MainViewController。 3)LogoutViewController。
I am using NSUserDefaults to store the username when he login from LoginViewController.
我从LoginViewController登录时使用NSUserDefaults来存储用户名。
Then in the AppDelegate I use this method:
然后在AppDelegate中我使用这个方法:
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let UserData = NSUserDefaults.standardUserDefaults()
if (UserData.stringForKey("Username") != nil) {
let initialViewCOntroller= storyboard.instantiateViewControllerWithIdentifier("MainViewController") as! MainViewController
self.window?.rootViewCOntroller= initialViewController
} else {
let initialViewCOntroller= storyboard.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController
self.window?.rootViewCOntroller= initialViewController
}
So, When there is Username stored in the NSUserDefaults, the app jump to Main View Controller and skip the first at App launch.
因此,当NSUserDefaults中存储了用户名时,应用程序将跳转到主视图控制器并跳过应用程序启动时的第一个。
in the LogoutViewController I used unwind segue to LoginViewController, meaning that I have a logout button and I used unwind segue to go to the LoginViewController. That works fine when the app starts from LoginViewController but not from MainViewController.
在LogoutViewController中我使用unwind segue到LoginViewController,这意味着我有一个注销按钮,我使用unwind segue转到LoginViewController。当应用程序从LoginViewController启动而不是从MainViewController启动时,这工作正常。
However, I want the to go to LoginViewController when Logout Button Pressed whether it starts from LoginViewController or MainViewController and release all other ViewControllers from memory. How can it be?
但是,我想在Logout Button Pressed时转到LoginViewController,无论它是从LoginViewController还是MainViewController启动,并从内存中释放所有其他ViewControllers。怎么会这样?
2 个解决方案