ios - 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 而不是“自我”展示,但这似乎并没有改变任何东西。
任何帮助将非常感激。
解决方案
正如错误所说,您需要从当前被解雇的 1 中提供一个 vc ,所以改为
self.dismiss(animated: true, completion: {
(UIApplication.shared.delegate as! AppDelegate).window?.rootViewController = loginVC
}
另一种方法是将 rootVC 嵌入到 navigationController 中并执行
self.navigationController?.setViewControlls([loginVC],animated:true)
推荐阅读
- sql - 如果没有要显示的结果,如何避免显示结果窗口?
- qt - 更改当前步进线背景颜色
- spring - REST端点的解释是否有顺序?
- artificial-intelligence - 用于 TSP 优化的 Max Min Ant 系统
- .net - 如何防止 MSI 主要升级执行旧版本的自定义操作
- batch-file - 在写入文本文件之前过滤搜索输出
- c - 了解 C 中的外部存储说明符
- hibernate - JPA插入连接表?
- javascript - JavaScript 处理多个过滤条件
- puppet - Puppet 错误:服务器上的错误 500:服务器错误:评估错误:评估函数调用时出错,找不到类