我一直在查看 Apple 文档和示例代码,试图确定管理 IBOutlet 内存的最佳方法。至少可以说,我有点困惑。
CurrentAddress 示例代码将 IBOutlets 声明为属性:
@interface MapViewController : UIViewController <MKMapViewDelegate, MKReverseGeocoderDelegate>
{
MKMapView *mapView;
UIBarButtonItem *getAddressButton;
}
@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *getAddressButton;
伟大的。这些在 dealloc 中释放:
- (void)dealloc
{
[mapView release];
[getAddressButton release];
[super dealloc];
}
现在这些属性不应该设置为分配吗?因为当设置为retain时,IBOutlet的retain计数将增加两次:一次是在加载nib时,另一次是在设置属性时?将这些属性设置为 nil 而不是在 dealloc 中释放不是更好吗?
Apple 文档说我们应该保留 iOS 的属性。
应释放保留的网点并nil
'ed 在两个dealloc
and viewDidUnload
.
在 Mac 上,加载 nib 时会自动保留未由超级视图保留的每个插座。 iOS 的情况并非如此。这就是为什么在视图层次结构中仅保留视图以外的出口在理论上是有效的。
Jeff LaMarche 关于这个主题有一篇非常有用的文章:奥特莱斯,Cocoa 与 Cocoa Touch http://iphonedevelopment.blogspot.com/search?q=iboutlet.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)