如何将rootViewController更改为尚未加载的VC?

时间:2017-07-29 19:21:42

标签: ios swift3 uiviewcontroller uistoryboard

想象一下像Twitter这样的应用程序,一旦你登录,你就永远不会再看到登录屏幕,直到你退出。我通过在启动时更改appDelegate中的初始viewController来完成此操作。但是,当要退出时,堆栈中的新VC下面没有任何内容可以解除。如何在初始视图加载后正确地将登录屏幕设置为rootViewController,以便我可以在注销时解除?

在appDelegate中我改变了initialVC,如下所示:

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let initialViewController = storyboard.instantiateViewController(withIdentifier: "RootVC")
        self.window?.rootViewController = initialViewController

然后我想在我的初始VC下面插入signInVC,以便我可以“解雇”它。

1 个答案:

答案 0 :(得分:0)

获取名为signup_status的bool值并将其存储在UserDefaults.standard中并将其设置为false

当用户登录时将此值更改为true,并且在他注销时make为false

现在在AppDelegate中检查用户是否登录

  if UserDefaults.standard.bool(forKey: "signup_status"){

  //if true the dont show login page and make home screen root view controller


      let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

        let viewController = storyBoard.instantiateViewController(withIdentifier: "homepage") as! homepageViewController
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.window?.rootViewController = viewController 




  }else {


 let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

        let viewController = storyBoard.instantiateViewController(withIdentifier: "login") as! login
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.window?.rootViewController = viewController 



 }

现在注销

为key signup_status false

设置UserDefaults.standard的值
     UserDefaults.standard.set(false, forKey: "signup_status")
     let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

    let viewController = storyBoard.instantiateViewController(withIdentifier: "login") as! login
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.window?.rootViewController = viewController