ios - 点击 tabBar 项目应该总是打开第一个 View Controller

标签 ios swift xcode uinavigationcontroller uitabbarcontroller

我有一个带有标签栏导航(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)
}

最佳答案

为了实现这个功能,我在tabBarDelegatedidSelect方法中编写了代码。

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/

相关文章:

ios - 为什么定时器/倒计时总是在 UITableViewCell 中重置为初始值

xcode - swift 错误 : Use of module 'CMSampleBuffer' as a type

javascript - WKWebView 弹出窗口未显示

ios - ViewController 对象为零

ios - 流委托(delegate)未调用

ios - 当子类化(也许?)到另一个节点时,SpriteKit Node 给我 nil | swift

ios - 更改 slider 上的动画速度

ios - 与 Objective-C 应用程序相比,捆绑的 Swift 库的文件大小开销是多少?

IOS UserDefault 无法存储在本地

iphone - 如何使用 UIGestureRecognizer 子类实现滑动