加载所有 TabBar 视图

发布于 2025-01-03 10:26:16 字数 122 浏览 0 评论 0原文

我有一个标签栏控制器作为根视图控制器。我想预加载选项卡 [1,2,3] 的视图(选项卡 0 自动加载为第一个选项卡)。

我本质上希望 viewdidload 中的代码在用户点击选项卡之前运行。

谢谢

I have a tabbar controller as the root view controller. I would like to pre-load the views of tab [1,2,3] (tab 0 loads as the first tab automatically).

I essentially would like the code in viewdidload to run before the user taps on the tab.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

困倦 2025-01-10 10:26:16

如果您将视图初始化代码移至 loadView 而不是 viewDidLoad 中,则只需调用 viewController.view 即可强制加载属于 UITabBarController 的每个 UIViewController。发生这种情况是因为 UIViewController 将在需要时通过 loadView 函数创建视图对象。

for(UIViewController * viewController in  tabBarController.viewControllers){
   viewController.view;
}

或者更简单地说

[tabBarController.viewControllers makeObjectsPerformSelector:@selector(view)];

If you take your view initialization code and move it into loadView instead of viewDidLoad you can force each of the UIViewControllers that are part of your UITabBarController to be loaded by simply calling viewController.view. This happens because a UIViewController will create the view object via the loadView function when asked for it.

for(UIViewController * viewController in  tabBarController.viewControllers){
   viewController.view;
}

or more simply

[tabBarController.viewControllers makeObjectsPerformSelector:@selector(view)];
万人眼中万个我 2025-01-10 10:26:16

在 iOS 8 中,我创建了一个名为 TSMainBarViewController 的 UITabViewController 子类。在 TSMainBarViewController 的函数 viewDidLoad 中,我刚刚添加了这样的代码:

for(UINavigationController * viewController in self.viewControllers){
    [[viewController.viewControllers firstObject] view];
}

然后所有的 viewController(UINavigationController 的根 viewController)都会加载。我所有的 viewController 都是在 StoryBoard 中创建的。不要在 UIViewController 中实现 loadView 函数。


来自 EligyD 的 Swift 3 代码:

for viewController in self.viewControllers! {
    _ = viewController.view
}

In iOS 8, I created a subclass of UITabViewController named TSMainBarViewController. In the function viewDidLoad of TSMainBarViewController, I just added this code:

for(UINavigationController * viewController in self.viewControllers){
    [[viewController.viewControllers firstObject] view];
}

Then all the viewControllers (root viewController of UINavigationController) will load . All my viewControllers are created in the StoryBoard. Do not implement the function loadView in the UIViewController.


Swift 3 code from EligyD:

for viewController in self.viewControllers! {
    _ = viewController.view
}
蝶…霜飞 2025-01-10 10:26:16

要处理可能有或没有导航控制器的选项卡控制器:

viewControllers?.forEach {
    if let navController = $0 as? UINavigationController {
        navController.topViewController?.view
    } else {
        $0.view.description
    }
}

To handle tab controllers which might or might not have navigation controllers:

viewControllers?.forEach {
    if let navController = $0 as? UINavigationController {
        navController.topViewController?.view
    } else {
        $0.view.description
    }
}
倾听心声的旋律 2025-01-10 10:26:16

如果标签栏控制器下的所有视图控制器都嵌入了导航栏:

Swift 3

for navViewController in self.viewControllers! {
    _ = navViewController.childViewControllers[0].view
}

User Panda 的解决方案不适合我。

If your all view controllers under the tab bar controller have a navigation bar embedded in them:

Swift 3

for navViewController in self.viewControllers! {
    _ = navViewController.childViewControllers[0].view
}

User Panda's solution didn't work for me.

宣告ˉ结束 2025-01-10 10:26:16

如果你的视图之前有 UINavigationController

for viewController in self.tabBarController!.viewControllers! {
    var aView = viewController.topViewController as UIViewController
    aView.view.description
}

if you have UINavigationController before your views

for viewController in self.tabBarController!.viewControllers! {
    var aView = viewController.topViewController as UIViewController
    aView.view.description
}
阳光的暖冬 2025-01-10 10:26:16

Swift 3:

在 UITabBarController 的 viewDidLoad()

for viewController in self.viewControllers! {
    _ = viewController.view
}

Swift 3:

In viewDidLoad() of your UITabBarController

for viewController in self.viewControllers! {
    _ = viewController.view
}
久隐师 2025-01-10 10:26:16

斯威夫特5:

viewControllers?.forEach {
  if let navController = $0 as? UINavigationController {
     let _ = navController.topViewController?.view
  } else {
      let _ = $0.view.description
    }
 }

Swift 5:

viewControllers?.forEach {
  if let navController = $0 as? UINavigationController {
     let _ = navController.topViewController?.view
  } else {
      let _ = $0.view.description
    }
 }
迷迭香的记忆 2025-01-10 10:26:16

斯威夫特等效...

for viewController in self.tabBarController.viewControllers!
{
    viewController.view;
}

Swift equivalent...

for viewController in self.tabBarController.viewControllers!
{
    viewController.view;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文