首页 > 解决方案 > Swift:在解雇的完成处理程序中呈现 VC

问题描述

我有以下 ViewController (VC) 流程:

SplashScreen/launch VC -> Login VC (on notification, present) -> SplashScreenVC -> Main VC

我想避免使用 unwind segue,因为无论当前的 VC 如何,我都需要定期重新验证用户身份,因此更愿意以编程方式“呈现”。

问题是,我能够展示和关闭 SplashScreen VC(它最初是根目录),但随后不能对 Login VC 执行相同操作而不会出现错误。

代码:

//in SplashScreen VC viewDidAppear

let loginVC = myStoryboard.instantiateViewController(identifier: "loginVC") as UIViewController
        loginVC.modalPresentationStyle = .fullScreen
        loginVC.modalTransitionStyle = .coverVertical

        //dismissal?
        self.dismiss(animated: true, completion: {
            self.present(loginVC, animated: true, completion: nil)
        })


//in loginVC selector function
let launchVC = myStoryboard.instantiateViewController(identifier: "launchVC") as UIViewController
        launchVC.modalPresentationStyle = .fullScreen
        launchVC.modalTransitionStyle = .coverVertical

        //check for top view controller (debugging)
        print("TOPVC at LoginVC: \(self.getTopVC()!)")

        //handle dismissal?
        self.dismiss(animated: true, completion: {
            self.present(launchVC, animated: true, completion: nil)
        })

警告:

Warning: Attempt to present <Slidr.LaunchScreenViewController: 0x15be0ef90> on <Slidr.LoginViewController: 0x15be6b510> whose view is not in the window hierarchy!

Warning: Attempt to present <Slidr.TestingViewController: 0x15db00ac0> on <Slidr.LaunchScreenViewController: 0x15bd06960> whose view is not in the window hierarchy!

如果我不关闭 loginVC,代码运行良好,但我想随着时间的推移避免残留控制器。

我试图从顶级 VC 而不是“自我”展示,但这似乎并没有改变任何东西。

任何帮助将非常感激。

标签: iosswiftuiviewcontrollersegue

解决方案


正如错误所说,您需要从当前被解雇的 1 中提供一个 vc ,所以改为

self.dismiss(animated: true, completion: {
   (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController = loginVC
}

另一种方法是将 rootVC 嵌入到 navigationController 中并执行

self.navigationController?.setViewControlls([loginVC],animated:true)

推荐阅读