如何自定义 UITabBarController 以更改打开 UIViewController

How to customise UITabBarController for changing opening UIViewController

我的 TabBarController 有两个项目:

第一项转到:

NavigationController1->ViewController11->ViewController12

第二项转到:

NavigationController2->ViewController21

我通过ViewController11转到ViewController12,然后单击item2,然后单击item1,我又回到ViewController12,但我想看ViewController11

我该怎么做?

尝试继承 UITabBarController:

import UIKit

class MVTabBarController: UITabBarController, UITabBarControllerDelegate {

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.delegate = self
    }

    func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
        var navigationController = viewController.navigationController
        if navigationController != nil {
            navigationController!.popToRootViewControllerAnimated(true)
        }
    }
}

在你的情节提要中:

试试这个,这只是一个想法,没有经过测试。

编辑

func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {

    if viewController.isKindOfClass(UINavigationController) {
        var navigationController = viewController as UINavigationController
        navigationController.popToRootViewControllerAnimated(true)
    }
}