This MUST have a [super viewWillAppear] method for viewWillAppear
I posted my code for iAd / AdMob ads in ...
-(void)viewWillAppear:(BOOL)animated{}
Ads are working fine as I have now on all iOS devices. When I connected my iPhone to Xcode and clicked on Product -->Analyze
, the message says ...
Instance method viewWillAppear:
in subclass UIViewController
"iPhoneSIX" missing call[super viewWillAppear:]
I just stumbled upon this thing by accident Product-->Analyze
. I really need to add [super viewWillAppear]
, although everything works fine on all devices as it currently is. Apple will reject my app if I ignore the problem navigator Product-->Analyze
?
Also, what does ...
[super viewWillAppear:YES];
What is causing this?
source to share
Apple doesn't get this specific when deciding whether to accept or reject your app. This only follows a guideline that doesn't affect the weeds of your specific methods that much.
Calling [super viewWillAppear:YES]
is best practice and I would recommend it. Always including super ensures that any code in the superclasses is called before any additional code is executed. Therefore, if you or someone else coded a superclass that expected some code to be executed, you are guaranteed to execute it, and not just rewrite the entire method in the subclass.
Let's say you have a type view controller MyViewController
that is a subclass UIViewController
. Then say that you have another type view controller MyOtherViewController
that is a subclass MyViewController
. Let's say you are now coding some things in viewWillAppear
to MyOtherViewController
. If you call super first, it will call viewWillAppear
in MyViewController
before executing any code. If viewWillAppear
in MyViewController
first calls super, then it will call viewWillAppear
in UIViewController
before executing any code.
source to share
According to Apple : (emphasis mine)
This method is called before the view receiver is added to the view hierarchy and before any animations are configured to show the view. You can override this method to perform custom view rendering tasks. For example, you can use this method of changing the orientation or style of the status bar for a coordinate with the orientation or style of the presented view. If you override this method, you must call super at some point in your implementation.
source to share
I'm sure Apple will not give up on your application unless you called the call super
on an overridden method, primarily because there are cases where you can specifically avoid calling super
.
However, as Josh Gaffney says, this is definitely the best practice to do this unless you have a compelling reason. Also be aware of some view controller subclasses (can't remember which ones, but maybe UICollectionViewController
) will only work correctly if their view lifecycle methods are called appropriately, so not calling super
can definitely break some classes (sometimes in subtle ways, which you cannot implement).
So my suggestion is to add a call super
(usually like the first line in a method) and see if everything continues to work fine. If not, spend a little time trying to figure out what's going on differently and see if you can solve it differently. In general, you should always (as a matter of habit) provide calls super
to any lifecycle methods that you override whenever possible.
source to share