xcode viewWillAppear 是否也必须具有 [super viewWillAppear] 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30243903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it a MUST for viewWillAppear to have [super viewWillAppear] method as well
提问by Jet
I placed my code for iAd/AdMob ads in...
我将 iAd/AdMob 广告的代码放在...
-(void)viewWillAppear:(BOOL)animated{}
Ads work perfectly fine the way I have them now on all iOS devices.
When I connected my iPhone to Xcode and clicked on Product -->Analyze
a message states...
广告工作得很好,就像我现在在所有 iOS 设备上使用它们的方式一样。当我将 iPhone 连接到 Xcode 并单击Product -->Analyze
一条消息时...
The viewWillAppear:
instance method in UIViewController
subclass 'iPhoneSIX' is missing a [super viewWillAppear:]
call
子类“iPhoneSIX”中的viewWillAppear:
实例方法UIViewController
缺少[super viewWillAppear:]
调用
I just accidentally stumbled upon this Product-->Analyze
thing. Do I really need to add [super viewWillAppear]
even though everything works perfectly fine on all devices as it currently is. Will Apple reject my app if I don't pay attention to the Product-->Analyze
issue navigator?
我只是不小心偶然发现了这个Product-->Analyze
东西。我是否真的需要添加,[super viewWillAppear]
即使在所有设备上一切正常,就像目前一样。如果我不注意Product-->Analyze
问题导航器,Apple 会拒绝我的应用程序吗?
Also, what does ...
还有,有什么...
[super viewWillAppear:YES];
What does calling this do?
调用这个有什么作用?
回答by j.f.
According to Apple: (emphasis mine)
根据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.
在将接收器的视图添加到视图层次结构之前以及在配置任何动画以显示视图之前调用此方法。您可以覆盖此方法以执行与显示视图相关的自定义任务。例如,您可以使用此方法更改状态栏的方向或样式,以与所呈现视图的方向或样式协调。 如果覆盖此方法,则必须在实现中的某个时刻调用 super。
回答by Josh Gafni
Apple doesn't gets that specific when deciding to Accept or Reject your app. It only follows the guidelines, which doesn't get that much into the weeds of your specific methods.
在决定接受或拒绝您的应用程序时,Apple 并没有具体说明。它只遵循指导方针,并没有对您的特定方法的杂草造成太大影响。
Calling [super viewWillAppear:YES]
is a best practice, and I would recommend it. Always including super ensures that any code in the super classes get called before executing any additional code. So if you or someone else coded a super class that expected some code to be executed, you are guaranteed to still execute it, rather than just overwriting the whole method in the subclass.
打电话[super viewWillAppear:YES]
是一种最佳做法,我会推荐它。始终包含 super 可确保在执行任何附加代码之前调用超类中的任何代码。因此,如果您或其他人编写了一个期望执行某些代码的超类,您可以保证仍然执行它,而不仅仅是覆盖子类中的整个方法。
Say you have a view controller of type MyViewController
which is a subclass of UIViewController
. Then say you have another view controller of type MyOtherViewController
, which is a subclass of MyViewController
. Say you're coding now some things in viewWillAppear
in MyOtherViewController
. If you call super first, it will call viewWillAppear
in MyViewController
before executing any code. If viewWillAppear
in MyViewController
calls super first, then it will call viewWillAppear
in UIViewController
before executing any code.
假设您有一个类型MyViewController
为UIViewController
. 然后假设您有另一个类型MyOtherViewController
为 的视图控制器,它是MyViewController
. 假设您现在viewWillAppear
在MyOtherViewController
. 如果你先调用 super,它会viewWillAppear
在MyViewController
执行任何代码之前调用。如果viewWillAppear
在MyViewController
调用中超第一,那么它会调用viewWillAppear
在UIViewController
执行任何代码之前。
回答by devios1
I'm quite certain Apple will not reject your app for failing to call super
on an overridden method, primarily because there are cases where you may specifically want to avoid calling super
.
我很确定 Apple 不会因为您的应用程序未能调用super
被覆盖的方法而拒绝您的应用程序,主要是因为在某些情况下您可能特别想避免调用super
.
That said, as Josh Gafni mentions it is definitely a best practice to do so, unless you have a very good reason for not. Also bear in mind some view controller subclasses (can't recall specifically which ones, but maybe UICollectionViewController
) will only work properly if their view lifecycle methods get called appropriately, so not calling super
can definitely break some classes (sometimes in subtle ways you may not realize).
也就是说,正如 Josh Gafni 提到的那样,这样做绝对是最佳实践,除非您有充分的理由不这样做。还要记住一些视图控制器子类(无法具体回忆哪些,但也许UICollectionViewController
)只有在它们的视图生命周期方法被适当调用时super
才能正常工作,因此不调用肯定会破坏某些类(有时以微妙的方式您可能没有意识到)。
Therefore my suggestion is add the call to super
(generally as the first line in the method) and see if things continue to work fine. If not, spend a bit of time trying to understand what is happening differently and see if you can solve it in a different way. In general you should always (as a force of habit) provide calls to super
on any view lifecycle methods you override whenever possible.
因此,我的建议是将调用添加到super
(通常作为方法的第一行)并查看是否继续正常工作。如果没有,请花一些时间尝试了解正在发生的不同情况,看看您是否可以以不同的方式解决它。一般来说,您应该始终(作为习惯的力量)提供super
对您覆盖的任何视图生命周期方法的调用。