热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

RootViewController和unwindsegues?-RootViewControllerandunwindsegues?

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 个解决方案

#1


1  

You need to set the LoginViewController as rootViewController on logout instead of unwinding the segue.

您需要在注销时将LoginViewController设置为rootViewController,而不是展开segue。

let initialViewCOntroller= storyboard.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController
UIApplication.sharedApplication().keyWindow?.rootViewCOntroller= initialViewController

#2


0  

In your case LoginViewController as root view is called first when app is launched. So when you log-out, you should pop to root view controller

在您的情况下,在启动应用程序时首先调用LoginViewController作为根视图。因此,当您注销时,您应该弹出到根视图控制器


推荐阅读
author-avatar
天蝎完美_主义
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有