简体   繁体   English

如何在UITabBarController中加载所有视图?

[英]How to load all views in UITabBarController?

I have not found relevant and up to date answers in the posts related to this question. 我没有在与此问题相关的帖子中找到相关和最新的答案。

I would like to load all viewcontrollers on launch. 我想在启动时加载所有viewcontrollers。 Currently it launches as expected but when I tap on a bar item (the first time) there is a slight delay to load it because it has not been loaded yet. 目前它按预期启动,但是当我点击一个条形项目(第一次)时,加载它有一点延迟,因为它尚未加载。

How can I do that is Swift ? 我怎么能这样做是Swift?

Thanks. 谢谢。

To preload a UIViewController 's view, simply access its view property: 要预加载UIViewController的视图,只需访问其view属性:

let _ = myViewController.view

To preload all view controllers on a UITabBarController , you can do: 要预加载UITabBarController上的所有视图控制器,您可以执行以下操作:

if let viewControllers = tabBarController.viewControllers {
    for viewController in viewControllers {
        let _ = viewController.view
    }
}

Or a bit more compactly: 或者更紧凑:

tabBarController.viewControllers?.forEach { let _ = $0.view }

Combining Robert's and M. Daigle's solution I came up with something like this: 结合Robert's和M. Daigle的解决方案,我提出了类似这样的事情:

for viewController in tabBarController?.viewControllers ?? [] {
    if let navigationVC = viewController as? UINavigationController, let rootVC = navigationVC.viewControllers.first {
        let _ = rootVC.view
    } else {
        let _ = viewController.view
    }
}

Add this to the ViewDidLoad of your first ViewController and should do the trick... 将它添加到你的第一个ViewController的ViewDidLoad,并应该这样做...

Robert's answer above worked for me, but I had UINavigationControllers as my initial view controllers for each tab. 罗伯特上面的回答对我有用,但我有UINavigationControllers作为每个标签的初始视图控制器。 Therefore I had to call 所以我不得不打电话

viewControllers.forEach { $0.view }

within my UINavigationControllers in order for their root view controllers to be loaded as well. 在我的UINavigationControllers中,也可以加载它们的根视图控制器。 Hope that helps Sam out. 希望能帮助Sam出局。

If you want to prepare both submissions and subcontrollers, you also have to call viewWillAppear(Bool) : 如果要准备提交和子控制器,还必须调用viewWillAppear(Bool)

tabBarController.viewControllers?.forEach {
    let _ = $0.view
    $0.viewWillAppear(true)
}

Ignoring this causes views will be under navigation and tab bars. 忽略此导致视图将在导航和标签栏下。

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

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