本文介绍了在我切换标签之前,viewWillAppear 不会为属于 UITabBarController 的 UIViewController 触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 iOS 应用程序中,我有一个 UITabBarController,它的 viewControllers 列表看起来像 [vc1, vc2],它们属于类MyVC1MyVC2 分别是 UIViewController 的子类.MyVC1 覆盖 viewWillAppear,被覆盖的版本会打印一些东西,所以我知道它什么时候被调用.(这是为了隔离我遇到的一个更大的问题,即没有出现视图.)

In my iOS app, I have a UITabBarController, and its viewControllers list looks like [vc1, vc2], which are of class MyVC1 and MyVC2 respectively, both of which subclass UIViewController. MyVC1 overrides viewWillAppear, and the overwritten version prints something so I know when it is called. (This is to isolate a larger problem I had, which was a view not appearing.)

我的问题是,当应用程序第一次启动并且 vc1 是选定的选项卡时,没有打印任何内容,这意味着它的 viewWillAppear 没有被调用.如果我切换标签然后返回到 vc1,会打印一些东西,所以 vc1viewWillAppear 在我切换回来之前不会被调用从另一个标签到它.

My issue is that when the app first starts up and vc1 is the selected tab, nothing is printed, meaning its viewWillAppear is not being called. If I switch tabs and then go back to vc1, something does get printed, so vc1's viewWillAppear is not being called until I switch back to it from another tab.

有什么方法可以让 vc1 在应用启动时立即调用 viewWillAppear 而无需切换标签页?老实说,我很惊讶这还不是默认行为.

Is there any way to have vc1 call viewWillAppear as soon as the app starts up, without needing to switch tabs? Honestly, I'm surprised this wasn't the default behavior already.

推荐答案

我刚刚在 Xcode 中创建了一个新的测试应用程序,在 Main.storyboard 中只有一个 UITabViewController(及其两个子视图控制器),当我覆盖 viewWillAppear 时第一个孩子,我的视图将出现"日志每次都会打印(包括在应用启动时).

I just made a new test app in Xcode with nothing but a UITabViewController (and its two child view controllers) in Main.storyboard, and when I override viewWillAppear for the first child, my "view will appear" log prints every time (including on app launch).

以下几点可能导致 viewWillAppear 不被调用:

Here are a couple things that could cause viewWillAppear to not be called:

  1. 不要在覆盖 viewWillAppear 的地方调用 super
  2. 在某处添加视图控制器的视图作为子视图而不将视图控制器添加为子视图控制器

您还可以尝试查看是否为您的 UITabBarController 调用了 viewWillAppear,如果没有,是否为其父级或呈现视图控制器调用了它?依此类推,直到您找到劫持的位置.

You also might try seeing if viewWillAppear is getting called for your UITabBarController, and if not, is it being called for its parent or presenting view controller? And so on until you find where the holdup is.

这篇关于在我切换标签之前,viewWillAppear 不会为属于 UITabBarController 的 UIViewController 触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 10:43