依赖注入到 UITabBarController 子 ViewControllers
Dependency injection into UITabBarController child ViewControllers
我目前正在为 iOS 设置标签栏应用程序。
通常情况下,我会在更改 viewController 时使用 prepareforSeque 等重写方法进行依赖注入,但当 UITabBarController 更改其活动子项时不会调用该方法 ViewController .如何正确地对 UITabBarController 子 ViewControllers 进行依赖注入?
经过一些额外的研究,我找到了答案。感谢 Will-m 提供我需要的线索。这个答案的当前 cavaet 是 TabBarController 加载的第一个视图不会被注入。
为了从 UITabBarController 向 ViewControllers 中注入数据,您需要执行以下操作:
首先,您需要在加载时将 RootViewController 设置为它自己的委托。
You don't necessarily need the controller class to be its own delegate
unless you need to inject the required data from another class directly to the UITabBarController.
您还需要声明委托 class 符合 UITabBarControllerDelegate 协议。
// Declare UITabBarControllerDelegate protocol
class RootViewController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Set class delegate to self
self.delegate = self
}
}
You need to set the RootViewController's delegate because the
delegate's protocol contains an important method:
tabBarController(_:shouldSelectViewController:). This method is
called when the RootViewController changes its active tab.
tabBarController(_:shouldSelectViewController:)的"viewController"参数是TabBarController切换到的childViewController的实例。如果你已经为 ViewController 分配了一个协议(这样编译器就知道你的变量是在 class 中声明的),你可以将变量注入 child.
所以将函数添加到 RootViewController class 像这样:
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) {
// Get your view controller using the correct protocol.
// Use guard to make sure the correct type of viewController has been provided.
guard let vc = viewController as? YourProtocol
else { fatalError("wrong view controller type") }
// Assign the protocol variable to whatever you want injected into the class instance.
vc.VariableInYourProtocol = InjectedVariable
}
就是这样。如果你需要支持不同协议的控制器,我可能会写一些关于使用 switch 语句来做到这一点的东西。那
到目前为止,这还不是我需要使用的东西。
此外,请注意,此方法适用于在活动 ViewController 之间仅传递一个 managedObjectContext 实例的 CoreData 实践。使用此方法而不是直接从每个 ViewController.
的应用程序委托中检索上下文的不同实例
在 RootViewController 的 viewDidLoad
中,您可以遍历 childViewControllers
并找到您想要的各种子控制器,并为每个子控制器设置依赖关系。在这种情况下,依赖项将在子视图控制器的 viewDidLoad 中可用。选项卡栏实例化子视图控制器实例,但在需要时才加载视图。
加载标签栏视图控制器后,您可以使用委托方法注入更新的依赖项并在 viewDidAppear
中使用它,因为 viewDidLoad
一旦在标签栏中被选中,就不会被调用。
我目前正在为 iOS 设置标签栏应用程序。
通常情况下,我会在更改 viewController 时使用 prepareforSeque 等重写方法进行依赖注入,但当 UITabBarController 更改其活动子项时不会调用该方法 ViewController .如何正确地对 UITabBarController 子 ViewControllers 进行依赖注入?
经过一些额外的研究,我找到了答案。感谢 Will-m 提供我需要的线索。这个答案的当前 cavaet 是 TabBarController 加载的第一个视图不会被注入。
为了从 UITabBarController 向 ViewControllers 中注入数据,您需要执行以下操作:
首先,您需要在加载时将 RootViewController 设置为它自己的委托。
You don't necessarily need the controller class to be its own delegate unless you need to inject the required data from another class directly to the UITabBarController.
您还需要声明委托 class 符合 UITabBarControllerDelegate 协议。
// Declare UITabBarControllerDelegate protocol
class RootViewController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Set class delegate to self
self.delegate = self
}
}
You need to set the RootViewController's delegate because the delegate's protocol contains an important method: tabBarController(_:shouldSelectViewController:). This method is called when the RootViewController changes its active tab.
tabBarController(_:shouldSelectViewController:)的"viewController"参数是TabBarController切换到的childViewController的实例。如果你已经为 ViewController 分配了一个协议(这样编译器就知道你的变量是在 class 中声明的),你可以将变量注入 child.
所以将函数添加到 RootViewController class 像这样:
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) {
// Get your view controller using the correct protocol.
// Use guard to make sure the correct type of viewController has been provided.
guard let vc = viewController as? YourProtocol
else { fatalError("wrong view controller type") }
// Assign the protocol variable to whatever you want injected into the class instance.
vc.VariableInYourProtocol = InjectedVariable
}
就是这样。如果你需要支持不同协议的控制器,我可能会写一些关于使用 switch 语句来做到这一点的东西。那 到目前为止,这还不是我需要使用的东西。
此外,请注意,此方法适用于在活动 ViewController 之间仅传递一个 managedObjectContext 实例的 CoreData 实践。使用此方法而不是直接从每个 ViewController.
的应用程序委托中检索上下文的不同实例在 RootViewController 的 viewDidLoad
中,您可以遍历 childViewControllers
并找到您想要的各种子控制器,并为每个子控制器设置依赖关系。在这种情况下,依赖项将在子视图控制器的 viewDidLoad 中可用。选项卡栏实例化子视图控制器实例,但在需要时才加载视图。
加载标签栏视图控制器后,您可以使用委托方法注入更新的依赖项并在 viewDidAppear
中使用它,因为 viewDidLoad
一旦在标签栏中被选中,就不会被调用。