Ios 在Swift中,当有5个以上的选项卡时,如何通过tabBarController中的restorationIdentifier设置代理?

Ios 在Swift中,当有5个以上的选项卡时,如何通过tabBarController中的restorationIdentifier设置代理?,ios,swift,delegates,Ios,Swift,Delegates,对于前四个选项卡,我使用restorationIdentifier为每个viewController设置适当的代理。在第5个和第6个选项卡中,restorationIdentifier是$MoreNavigationController$,因为您不直接跳到viewController。我没有很好地陈述我的问题,因此它值得投反对票。我花了比我预期的长得多的时间,但我找到了答案: 但既然那是用obj-C写的,我想我会分享我写的Swift版本 func tabBarController(tabBarC

对于前四个选项卡,我使用restorationIdentifier为每个viewController设置适当的代理。在第5个和第6个选项卡中,restorationIdentifier是$MoreNavigationController$,因为您不直接跳到viewController。

我没有很好地陈述我的问题,因此它值得投反对票。我花了比我预期的长得多的时间,但我找到了答案:

但既然那是用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
    }
}