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

≯℡__Kan透↙ 提交于 2019-12-06 01:59:37

问题


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?


回答1:


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




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/804539/which-top-level-objects-is-apple-talking-about-in-the-memory-management-progra

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!