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

时间:2015-03-18 08:30:52

标签: ios swift uitabbarcontroller

我有TabBarController有两个项目:

第一项指向:

NavigationController1->ViewController11->ViewController12

第二项目转到:

NavigationController2->ViewController21

我通过ViewController12转到ViewController11,然后点击第2项,然后点击第1项,然后我又回到ViewController12,但我希望看到ViewController11

我该怎么办?

1 个答案:

答案 0 :(得分:0)

尝试继承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)
        }
    }
}

在你的故事板中:

enter image description here

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

修改

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

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