简体   繁体   English

是否可以在 UITabBarController 上设置协调器属性?

[英]Is it possible to set a coordinator property on a UITabBarController?

I'm trying to implement a slide out menu in my tab-based app.我正在尝试在基于选项卡的应用程序中实现滑出式菜单。 I have a container controller which holds the MenuViewController and the UITabBarController, and it conforms to a MasterDelegate protocol which tells the container to slide out the MenuViewController when the hamburger button located on an individual tab is pressed.我有一个包含 MenuViewController 和 UITabBarController 的容器控制器,它符合 MasterDelegate 协议,当按下位于单个选项卡上的汉堡按钮时,它告诉容器滑出 MenuViewController。 My problem is that the coordinator property for the UITabBarController is not set until after the UITabBarController is configured, and thus the property in the UITabBarController as well as the ViewController for the tab containing the button is always nil.我的问题是 UITabBarController 的 coordinator 属性在配置 UITabBarController 之后才设置,因此 UITabBarController 中的属性以及包含按钮的选项卡的 ViewController 始终为零。

Here is the code belonging to the container that sets up the UITabBarController:这是属于设置 UITabBarController 的容器的代码:

func configureHomeController() {

    centerController = MainTabBarController()

    // Make sure to set delegate of homeController
    centerController.masterDelegate = self

    print("MainContainterController: Setting the MainTabBarController delegate")

    if centerController.masterDelegate != nil {
    print("MainContainerController: MainTabBarController Delegate set")
    }

    view.addSubview(centerController.view)

    // Add the navigationController containing the HomeController as a child to the ContainerController
    addChild(centerController)

    // Home controller didMove to the parent container of self, which is the ContainerController
    centerController.didMove(toParent: self)
}

In the TabBarController, there is a print statement that executes after the TabBarController is configured which tells me if the masterDelegate property is nil or not.在 TabBarController 中,有一个打印语句在 TabBarController 配置后执行,它告诉我 masterDelegate 属性是否为 nil。 This is printed before the container controller prints the two above statements.这是在容器控制器打印上述两个语句之前打印的。 Since the 2nd print statement is printed, I know that the coordinator is being set, but it's set too late.由于打印了第二个打印语句,我知道协调器正在设置,但设置为时已晚。

My question is how do I get the masterDelegate to be set before the UITabBarController is done being configured?我的问题是如何在完成配置 UITabBarController 之前设置 masterDelegate? Or is there a way to set that property after the fact?或者有没有办法在事后设置该属性?

Create a convenience init for your tabbarcontroller and pass the delegate to it.为您的 tabbarcontroller 创建一个方便的 init 并将委托传递给它。 Something like this:像这样的东西:

convenience init(masterDelegate: MasterDelegate) {
    self.init(nibName: nil, bundle: nil)

    self.masterDelegate = masterDelegate
}

the when you are setting up the TabBarController, instantiate it like this:当你设置 TabBarController 时,像这样实例化它:

   centerController = MainTabBarController(masterDelegate: self)
   centerController.configure()

Also, make a configure function in MainTabBarController.另外,在 MainTabBarController 中创建一个配置函数。 In there, set up your viewControllers.在那里,设置您的 viewControllers。

 (in MainTabBarController)
   func configure() {
       viewControllers = [viewController, viewController2, ....]
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM