2

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

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

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

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

4

4 回答 4

9

谢谢 Or Arbel,您实际上帮助我理解了这个问题。我不得不调用viewUINavigationController 中的第一个 UIViewController。这是有效的代码:

UITabBarController * tabBarController = (UITabBarController *)self.centerController;
NSArray *myViewControllers = tabBarController.viewControllers;
for (UINavigationController *navViewController in myViewControllers)
{
    [[navViewController.viewControllers objectAtIndex:0] view];
}
于 2012-12-14T23:51:51.663 回答
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];
}
于 2015-05-22T22:39:10.840 回答
1

尝试

UITabBarController * tabBarController = (UITabBarController *)self.centerController;
NSArray *myViewControllers = tabBarController.viewControllers;
for (UINavigationController *navViewController in myViewControllers)
{
    [navViewController view];
}
于 2012-12-14T22:40:13.580 回答
0
extension UITabBarController {
    func preload() {
        children.forEach { _ = $0.view }
    }
}
于 2021-05-04T11:48:08.610 回答