手动添加ViewController进入堆栈swift 3

时间:2017-07-13 07:56:52

标签: ios swift3 uiviewcontroller uinavigationcontroller

我正在编写我的应用中的退出按钮。这是我应用的流程。

  1. SplashvwController - > secretCodeViewController - > LoginviewController - > DashboardViewController
  2. 在此DashboardViewController我有signOut按钮。

    一旦用户登录,我的应用就会有单点登录功能,下次打开应用时

    1. SplashvwController - > DashboardViewController
    2. 我想在用户点击退出按钮时提示用户Loginviewcontroller

      问题

      当用户浏览路径1时,我只需执行popviewcontroller即可返回上一个viewcontroller。但是,当用户通过第二条路径时,

      • 如何手动将Loginviewcontroller添加到我的 viewcontrollers stack执行相同的操作?
      • 如何检查当前Viewcontrollers堆栈中是否存在LoginviewController?

      请帮帮我

2 个答案:

答案 0 :(得分:0)

我认为,以下代码可以帮助您,

 for (var i = 0; i < self.navigationController?.viewControllers.count; i++) {
                        if(self.navigationController?.viewControllers[i].isKindOfClass(Loginviewcontroller) == true) {

                            self.navigationController?.popToViewController(self.navigationController!.viewControllers[i], animated: true)

                            break;
                        }
                    }
  

要手动添加ViewController,请检查以下代码。

     let navigation = mainStoryboard.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController 
    let nav = UINavigationController(rootViewController: navigation) 
appdelegate.window!.rootViewController = nav

答案 1 :(得分:0)

identifier文件上为LoginViewController设置情节提要参考Main.Storyboard

每当您想要显示LoginViewController时,只需调用函数

  func launchLoginView() {
        if let loginVC: LoginViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("LoginStoryboardID") as? LoginViewController {

        // .instantiatViewControllerWithIdentifier() returns AnyObject! this must be downcast to utilize it
       // LoginStoryboardID is the reference id for login view controller.


            self.presentViewController(loginVC, animated: true, completion: nil). 
            // OR
            //UIApplication.shared.keyWindow?.rootViewController = loginVC
        }
    }

根据用户登录状态登陆屏幕。

AppDelegte.swift

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

       func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

            let isLoggedAlready = //Get the login status
             if isLoggedAlready == true {
                if let dashBoardVC: DashboardViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("DashboardStoryboardID") as? DashboardViewController {
                     //Already logged in then directly launch dashboard vc.

                    //change the code based on your needs
                      window?.rootViewController =  dasbBoardVC

                }
             } 
        // Otherwise let it go as flow 1

        return true

   } 
}