swift - Swift - 在另一个标签栏项目上关闭视图控制器
问题描述
我有一个标签栏应用程序,每个标签都有一些显示segue。我希望能够从另一个选项卡栏中的操作中关闭一个选项卡栏中的视图控制器。
在我的示例中,当用户点击注销时,我想关闭每个选项卡中所有呈现的视图控制器。
标签栏控制器是根视图控制器:
let tabBarController = self.window!.rootViewController as? UITabBarController
我不确定我哪里出错了,因为我尝试过的以下许多代码片段对我不起作用......
self.navigationController?.popToRootViewController(animated: true)
self.view.window?.rootViewController?.dismiss(animated: false, completion: nil) //doesn't dismiss presented VC's on other tab bar controller
UIApplication.shared.keyWindow?.rootViewController?.dismiss(animated: false, completion: nil)
self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)
dismissViewControllers()
dismiss(animated: false, completion: nil)
}
func dismissViewControllers() {
guard let vc = self.presentingViewController else { return }
while (vc.presentingViewController != nil) {
vc.dismiss(animated: true, completion: nil)
}
}
呈现的视图控制器仍在堆栈上。我在这里有什么明显的遗漏吗?
解决方案
为此,您需要实现的是如何在选择注销选项卡项时重置根视图控制器。要知道在 中选择了哪个项目,UITabBarController
您需要检测何时按下选项卡栏项目。
理论/伪代码:
通过组合以上几点,这是一个理论示例:
class SpecialTabBarController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
}
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if let _ = viewController as? LogoutViewController {
// Reset root view controller of the UIWindow
// And must call makeKeyAndVisible() on the UIWindow object
}
}
}
顺便说一句,如果从 Interface Builder 中使用,请不要忘记将其设置SpecialTabBarController
为 Tab Bar Controller 的自定义类。
奖金:
这是一个完整的解决方案,您可以检查一下,尽管由于iOS 13 中引入的SceneDelegate 和 AppDelegate 之间SpecialTabBarController
的差异,需要考虑更多因素。
推荐阅读
- c++ - 如何通过 glibc/malloc 回溯调试崩溃的 Linux 程序(无可用内核)?
- javascript - 使用数组属性设置嵌套对象属性的正确方法
- python - 在 Python 中无法中止 (ctrl+c)
- nativescript - NativeScript Angular 文本字段 - 将“安全”属性从 true 更改为 false 时,光标距离文本太远(iOS)
- python-3.x - 如何将RGB图像转换为黑白
- html - 我正在寻找一个内部
加下划线并填充剩余空间
标签
- scala - Spark - 对 RDD 中的双精度值进行排序并忽略 NaN
- sqlite - 无法解析方法 super 以尝试创建 SQL lite 数据库(Android Studio 初学者问题)
- apache-spark - Apache Spark AWS S3 跟踪已处理文件的状态
- r - 文本相似度 - 余弦 - 控制