Which “Top-Level Objects” is Apple talking about in the Memory Management Programming Guide?

StackOverflow https://stackoverflow.com/questions/804539

  •  03-07-2019
  •  | 
  •  

Question

In the Memory Management Programming Guide for Cocoa Apple talks about Top-Level Objects. They say, that I need an Outlet for each of them.

If there are any top-level objects you do not store in outlets, however, you must retain either the array returned by the loadNibNamed:owner:options: method or the objects inside the array to prevent those objects from being released prematurely.

So what exactly do they mean with "top-level object"? I would say they talk about the root view and window. What else? And is this hint just for cases in which I would want to load their nib manually? Or does it apply for any nib and any case?

Was it helpful?

Solution

Top-level objects are objects that appear in the main nib window in Interface Builder, other than File’s Owner and Application.

OTHER TIPS

Yes, this is about the cases where you load the Nib manually, else you wouldn't have called loadNibNamed:owner:options:.

Objects that appear in the window with File's Owner, First Responder, View are top level objects. Anything you add to the view will be a subview - all subviews are retained by their direct superview, so they don't need to be retained elsewhere. The view itself is retained by the view controller, so that doesn't need to be retained.

If you add non-view objects, or views that you don't put into your main view right away (such as tool bar buttons that aren't currently showing) you need to retain them, or they will get released. My preferred method for doing this is with IBOutlets using @property, like this:

@property(nonatomic, retain) IBOutlet UIBarButtonItem * myButton;

This causes -setMyButton: to be called on file's owner (if this code is in file's owner) and the object connected through IB is sent to be stored in this property.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top