Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

weak IBOutleCollection is always nil

My question is why weak IBOutletCollection is always nil? If change weak to strong all my buttons are there, it's just really weird. I'm trying to understand apple's logic and I can see no difference between a single button and an array of buttons in terms of memory management. Am I missing something?

like image 275
dariaa Avatar asked Feb 25 '13 17:02

dariaa


People also ask

Should IBOutlets be weak or strong?

The official answer from Apple is that IBOutlets should be strong. The only case when an IBOutlet should be weak is to avoid a retain cycle. A strong reference cycle can result in memory leaks and app crashes.

Why are outlets weak IOS?

Because the outlet is declared weak, it is automatically set to nil when it's deallocated. Because the outlet is an implicitly unwrapped optional, it has no value, that is, is equal to nil , and the application would crash as a result. If this happens, chances are you're doing something you shouldn't be doing.

What is @IBOutlet in Swift?

Swift code is associated with graphical interface elements through the use of outlets and actions. An IB Outlet (short for Interface Builder outlet) is a graphical component that your code links to. An IB action is the reverse: It is a method in your code that a graphical component links to.


2 Answers

In no way complete, but simple answer:

A single UIButton created with IB is automatically a subView of some other UIView (at least the .view of your UIViewController) and is pointed strongly to because of that.

An IBOutletCollection is a NSArray or NSMutableArray, not a UIView displayed anywhere and UIViews obviously have no property pointing to Outlet(Collection)s that point to them, so nothing is pointing to IBOutletcollections. You have to do that yourself.

like image 108
blub Avatar answered Oct 11 '22 13:10

blub


From Apple's Resource Programming Guide:

Each time you ask the NSBundle or NSNib class to load a nib file, the underlying code creates a new copy of the objects in that file and returns them to you. (The nib-loading code does not recycle nib file objects from a previous load attempt.) You need to ensure that you maintain the new object graph as long as necessary, and disown it when you are finished with it. You typically need strong references to top-level objects to ensure that they are not deallocated; you don’t need strong references to objects lower down in the graph because they’re owned by their parents, and you should minimize the risk of creating strong reference cycles.

From a practical perspective, in iOS and OS X outlets should be defined as declared properties. Outlets should generally be weak, except for those from File’s Owner to top-level objects in a nib file (or, in iOS, a storyboard scene) which should be strong. Outlets that you create should therefore typically be weak, because:

Outlets that you create to subviews of a view controller’s view or a window controller’s window, for example, are arbitrary references between objects that do not imply ownership. The strong outlets are frequently specified by framework classes (for example, UIViewController’s view outlet, or NSWindowController’s window outlet).

@property (weak) IBOutlet MyView *viewContainerSubview;
@property (strong) IBOutlet MyOtherClass *topLevelObject;

And further down the page:

Outlets should be changed to strong when the outlet should be considered to own the referenced object:

  • As indicated previously, this is often the case with File’s Owner—top level objects in a nib file are frequently considered to be owned by the File’s Owner.
  • You may in some situations need an object from a nib file to exist outside of its original container. For example, you might have an outlet for a view that can be temporarily removed from its initial view hierarchy and must therefore be maintained independently.
like image 37
mbuc91 Avatar answered Oct 11 '22 13:10

mbuc91