在Swift中,如果有超过5个选项卡,如何在tabBarController中通过restorationIdentifier设置委托?

时间:2015-07-10 13:49:30

标签: ios swift delegates

对于前四个选项卡,我使用restorationIdentifier为每个viewController设置正确的委托。对于第5和第6个选项卡,restoreIdentifier是 $ MoreNavigationController $ ,因为您不直接跳转到viewController。

1 个答案:

答案 0 :(得分:0)

我没有很好地陈述我的问题,所以它应该获得下来的选票。我花了比预期更长的时间,但我找到了答案:didSelectViewController not getting called when in "More" section

但是因为那是用obj-C编写的,我以为我会分享我写的Swift版本。

func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
    // If the user hits the "More" tab for the first time, we need to set up tabController as a delegate for More's navigationController
    if (viewController == tabBarController.moreNavigationController && tabBarController.moreNavigationController.delegate == nil) {
        tabBarController.moreNavigationController.delegate = self
    }
    setUpDelegates(viewController)
}

// This is called when the user chooses something from the list from “More”
func navigationController(navigationController: UINavigationController, didShowViewController viewController: UIViewController, animated: Bool) {
    setUpDelegates(viewController)
}

// This manages setting up all view controllers with delegates or whatever when they are selected
func setUpDelegates(viewController: UIViewController) {
    if viewController.restorationIdentifier == “foo” {
        let fooController = viewController as? FooController
        fooController?.delegate = dataManager
    } else if viewController.restorationIdentifier == “bar” {
        let barController = viewController as? BarController
        barController?.delegate = dataManager
    }
}