iOS - 管理IBOutlets内存的最佳方法是什么? [英] iOS - What is best way to manage memory for IBOutlets?

查看:80
本文介绍了iOS - 管理IBOutlets内存的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在审核Apple文档和示例代码,以尝试确定管理IBOutlet内存的最佳方法。至少可以说,我有点困惑。

I've been reviewing the Apple docs and sample code to try to determine the best way to manage memory for IBOutlets. I'm a little confused, to say the least.

CurrentAddress示例代码将IBOutlets声明为属性:

The CurrentAddress sample code declares IBOutlets as properties:

@interface MapViewController : UIViewController <MKMapViewDelegate, MKReverseGeocoderDelegate>

{
    MKMapView *mapView;
    UIBarButtonItem *getAddressButton;
}
@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *getAddressButton;

太棒了。这些是在dealloc中发布的:

Great. And these are released in dealloc:

- (void)dealloc
{
    [mapView release];
    [getAddressButton release];
    [super dealloc];
}

现在不应将这些属性设置为分配?因为当设置为retain时,IBOutlet的保留计数将增加两次:一次加载nib时和另一次设置属性时?将这些属性设置为nil而不是在dealloc中释放不是更好吗?

Now shouldn't these properties be set to assign? Because when set to retain, the IBOutlet's retain count will be increased twice: once when the nib is loaded and another time when the property is set? And wouldn't it be better to set these properties to nil instead of releasing in dealloc?

推荐答案

Apple文档说我们应该保留iOS的属性。

应该在 dealloc 中释放保留的商店并且 nil '和 viewDidUnload

Apple docs says we should retain properties for iOS.
Retained outlets should be released and nil'ed in both dealloc and viewDidUnload.

在Mac上,加载笔尖时,超级视图未保留的每个插座都会自动保留。 iOS的情况并非如此。这就是为什么理论上只保留视图层次结构中视图以外的出口的原因。

On the Mac, every outlet which is not retained by a superview is automatically retained when loading the nib. That's not the case with iOS. That's why it's theoretically valid to only retain outlets other than views in the view hierarchy.

Jeff LaMarche就这个主题发表了一篇非常有用的帖子: Outlets,Cocoa vs. Cocoa Touch

There's a very helpful post by Jeff LaMarche regarding this topic: Outlets, Cocoa vs. Cocoa Touch.

这篇关于iOS - 管理IBOutlets内存的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆