Swift: 在解雇的完成处理程序中呈现 VC

Swift: Presenting VC in completion handler of dismissal

我有以下 ViewController (VC) 流程:

SplashScreen/launch VC -> 登录 VC(收到通知,出现) -> SplashScreenVC -> Main VC

我想避免使用 unwind segue,因为无论当前 VC 是什么,我都需要定期重新验证用户,因此更愿意以编程方式 'present'.

问题是,我可以显示和关闭 SplashScreen VC(最初是根目录),但无法对登录 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!

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

我尝试从顶部 VC 而不是 'self' 进行展示,但这似乎没有任何改变。

如有任何帮助,我们将不胜感激。

正如错误所说,您需要从当前已关闭的 1 中呈现 vc,因此改为

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

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

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