我的应用程序有一个 TabBarController。每个 tabBarItem 都与嵌入在 NavigationController 中的 ViewController 相关。
在第一个 tabBarItem 并选择另一个 tabBarItem 时,我想在移动到所选 ViewController 之前做一些事情。因此,我为我的 tabBarController 创建了一个类并将其设为 UITabBarControllerDelegate。
我想要做的是用两个按钮显示一个警报;按钮 A 取消移动到选定的 viewController,按钮 B 让移动发生。
我的问题是,当按下按钮 B 时,我想 popToRootViewController。我给了 navigationController 一个 storyboardID 并尝试实例化它,如下所示。
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if let alert = self.storyboard?.instantiateViewController(withIdentifier: "ActiveSessionWarningAlert") as? ActiveSessionWarningAlert {
alert.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
alert.modalTransitionStyle = UIModalTransitionStyle.flipHorizontal
let alertWasDismissed: (Bool) -> Void = { userWantsToMoveToSelectedViewController in
if userWantsToMoveToSelectedViewController {
if let navContr = self.storyboard?.instantiateViewController(withIdentifier: "firstNavContr") as? UINavigationController {
navContr.popToRootViewController(animated: true)
}
tabBarController.selectedViewController = viewController
}
}
alert.alertWasDismissed = alertWasDismissed
self.present(alert, animated: true, completion: nil)
}
return false
}
一切都按预期工作,但 popToRootViewController 似乎没有发生;当再次选择第一个 tabBarItem 时,我们离开该项目时处于“活动”状态的同一 viewController 仍在显示。
我检查了我想要弹出的 viewController 实际上在导航堆栈中,并且 navContr != nil。
我错过了什么?