从UITabBar预加载视图

时间:2012-12-14 21:34:35

标签: ios uitabbarcontroller preload

我有一个加载UITabBarController的类。每个选项卡都会打开一个UINavigationController。

我正在尝试在UINavigationControllers中预加载视图。我试过这样做:

UITabBarController * tabBarController = (UITabBarController *)self.centerController;
NSArray *myViewControllers = tabBarController.viewControllers;
for (UINavigationController *navViewController in myViewControllers)
{
    [navViewController loadView];
}

我尝试了不同的东西,但它永远不会被加载。我做错了吗?

4 个答案:

答案 0 :(得分:9)

谢谢或者Arbel,你实际上帮助我理解了这个问题。我不得不在UINavigationController内的第一个UIViewController上调用view。以下是有效的代码:

UITabBarController * tabBarController = (UITabBarController *)self.centerController;
NSArray *myViewControllers = tabBarController.viewControllers;
for (UINavigationController *navViewController in myViewControllers)
{
    [[navViewController.viewControllers objectAtIndex:0] view];
}

答案 1 :(得分:2)

如果您使用自动布局,则必须告诉视图相应地布局其子视图。

试试这个

UITabBarController * tabBarController = (UITabBarController *)self.centerController;
NSArray *myViewControllers = tabBarController.viewControllers;
for (UINavigationController *navViewController in myViewControllers) {
     UIViewController *ctrl = navViewController.topViewController;
     [ctrl.view setNeedsLayout];
     [ctrl.view layoutIfNeeded];
}

答案 2 :(得分:1)

尝试

UITabBarController * tabBarController = (UITabBarController *)self.centerController;
NSArray *myViewControllers = tabBarController.viewControllers;
for (UINavigationController *navViewController in myViewControllers)
{
    [navViewController view];
}

答案 3 :(得分:0)

extension UITabBarController {
    func preload() {
        children.forEach { _ = $0.view }
    }
}