viewWillAppear 是否也必须具有 [super viewWillAppear] 方法

Is it a MUST for viewWillAppear to have [super viewWillAppear] method as well

我将 iAd/AdMob 广告的代码放在...

-(void)viewWillAppear:(BOOL)animated{}

广告与我现在在所有 iOS 设备上使用的方式一样完美。 当我将 iPhone 连接到 Xcode 并单击 Product -->Analyze 时,一条消息指出...

UIViewController 子类 'iPhoneSIX' 中的 viewWillAppear: 实例方法缺少 [super viewWillAppear:] 调用

我只是无意中发现了这个 Product-->Analyze 东西。我真的需要添加 [super viewWillAppear] 吗,即使一切都像目前一样在所有设备上运行得很好。如果我不注意 Product-->Analyze 问题导航器,Apple 会拒绝我的应用程序吗?

此外,什么是...

[super viewWillAppear:YES];

调用这个有什么作用?

Apple 在决定接受或拒绝您的应用程序时并没有具体说明。它仅遵循指南,并没有过多涉及您的特定方法。

调用 [super viewWillAppear:YES] 是最佳做法,我会推荐它。始终包含 super 可确保在执行任何其他代码之前调用 super classes 中的任何代码。因此,如果您或其他人编写了一个期望执行某些代码的 super class,您可以保证仍然执行它,而不是仅仅覆盖 subclass.[=23= 中的整个方法]

假设您有一个 MyViewController 类型的视图控制器,它是 UIViewController 的子 class。然后假设您有另一个 MyOtherViewController 类型的视图控制器,它是 MyViewController 的子 class。假设您现在正在 MyOtherViewController 中编写 viewWillAppear 中的一些内容。如果您先调用 super,它会在执行任何代码之前在 MyViewController 中调用 viewWillAppear。如果 MyViewController 中的 viewWillAppear 先调用 super,那么它将在执行任何代码之前调用 UIViewController 中的 viewWillAppear

根据Apple:(强调我的)

This method is called before the receiver's view is about to be added to a view hierarchy and before any animations are configured for showing the view. You can override this method to perform custom tasks associated with displaying the view. For example, you might use this method to change the orientation or style of the status bar to coordinate with the orientation or style of the view being presented. If you override this method, you must call super at some point in your implementation.

我很确定 Apple 不会拒绝您的应用程序未能在重写方法上调用 super,主要是因为在某些情况下您可能特别希望避免调用 super

也就是说,正如 Josh Gafni 提到的那样,这样做绝对是最佳做法,除非您有充分的理由不这样做。还要记住一些视图控制器 sub类(记不起具体是哪些,但也许 UICollectionViewController)只有在正确调用它们的视图生命周期方法时才能正常工作,所以不要调用 super 绝对可以打破一些 类(有时您可能没有意识到的微妙方式)。

因此,我的建议是添加对 super 的调用(通常作为方法中的第一行),看看是否一切正常。如果没有,请花点时间尝试了解发生了什么不同的事情,看看是否可以用不同的方式解决它。通常,您应该始终(作为一种习惯)在您覆盖的任何视图生命周期方法上提供对 super 的调用。