Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I still need to set IBOutlet properties to null in viewDidUnload with ARC

Do I still need to set IBOutlet properties to null in viewDidUnload with ARC?

Because it still generates the following comment:

// Release any retained subviews of the main view.

like image 620
TheLearner Avatar asked May 17 '12 15:05

TheLearner


2 Answers

Well the main purpose of nilling outlet it was to do not create zombies, leaks and weird situations that could happen when the subviews have not a super view, while the view was unloaded from the view controller.

Now with the latest version of Xcode if you drag a view element inside a header or in a private declaration it sets automatically the Outlet to weak (targeting iOS>=5) and also in the viewDidUnload method it will write [self setYourOutlet:nil]; probably in this case is not necessary, but is a good practice. If you target lower ioses is needed, because you can't use weak reference. I suggest to use always because is a good habit.

UPDATE

I'd like to complete the answer to avoid misunderstandings (talking about iOS5 only) pay attention that IB sets outlet to weak only if the are subviews of a main view. Typically it happens in a xib containing a view from a view controller.

Sometimes could happen that you need to swap two views based on some condition at runtime without creating them programmatically or in different xibs. For instance you have your main view owned by the vc, and in the same xib you create two other views that in that moment doesn't have a superview. If you try to connect them with the same technique the reference created will be strong. At runtime you can then now swap the views simply adding or removing from the superview of course you should nil them in viewDidUnload.

like image 56
Andrea Avatar answered Oct 20 '22 00:10

Andrea


I'll expand on Andrea's answer here (upvote him!) because the answer isn't straight forward unless you only mean UI components, in which case they should all be weak.

IBOutlets are whatever you define them. If you use:

@property (nonatomic, strong) IBOutlet UIView *someView;

You should nil this when unloading the parent view/window.

If you do:

@property (nonatomic, weak) IBOutlet __weak UIView *someView;

You don't have to nil the variable, because it will be auto zero'ed.

How you nil is entirely up to you. Prior to ARC I used:

[someView_ release], someView_ = nil;

Now you have two options: either use the setter (created with with @synthesize) or set the underlying ivar directly. The result is the same - in both instances the object's lifetime qualifiers will note it's final use and release it.

So, go ahead and do this:

self.someView = nil

or

@synthesize someView = someView_;
...
someView_ = nil;
like image 29
Matt Melton Avatar answered Oct 20 '22 00:10

Matt Melton