我有一个带有标签栏导航(5 个按钮)的应用程序。点击 5 个选项卡栏按钮中的一个时,是否可以始终打开导航的第一个 Controller ?例如,button1 打开 VC1(带导航 Controller ),VC1 可以打开 VC2,VC2 可以打开 VC3,等等。如果我在 VC3 上,则单击另一个选项卡栏按钮(假设是 button2),然后再次单击 button1,我希望它打开 VC1,而不是我离开它的地方的 VC3。
我试过这个,但由于某种原因它没有像我预期的那样工作:
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
self.navigationController?.popToRootViewController(animated: true)
viewController.navigationController?.popToRootViewController(animated: true)
}
最佳答案
为了实现这个功能,我在tabBarDelegate
的didSelect
方法中编写了代码。
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
let rootView = self.viewControllers![self.selectedIndex] as! UINavigationController
rootView.popToRootViewController(animated: false)
}
关于ios - 点击 tabBar 项目应该总是打开第一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52178000/