使用loadNibNamed:方法时是否需要释放IBOutlets? [英] Do I need to release IBOutlets when using loadNibNamed: method?

查看:103
本文介绍了使用loadNibNamed:方法时是否需要释放IBOutlets?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.xib文件,其中包含一个UIView和2个UILabel子视图,这些子视图链接到一个名为Note的类,其中适当地分配给每个标签的出口,该类的定义包含以下内容。

I have a .xib file containing a UIView and 2 UILabel subviews linked to a class named Note with outlets assigned to each label appropriately, the definition for this class contains the following.

@interface Note : UIView {
    IBOutlet UILabel *time;
    IBOutlet UILabel *content;
}

我正在使用以下代码构建此代码

I'm constructing this with the following code

NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"Note" owner:self options:nil];
note = [nibViews lastObject];
[self addSubview:note];

现在,在我的Note类dealloc阶段,我没有发布时间或内容,但我我想知道我是否应该这样做?

Now, in my Note class dealloc phase, I'm not releasing either time or content, but I'm wondering if I should?

- (void)dealloc {
    [super dealloc];
}

我假设我没有,因为我没有明确保留这些我的代码中的任何地方的对象,我不会将它们合成到getter / setter中。但是我对nib unarchiving知道我是否应该在dealloc阶段发布这些内容还不够了?

I'm assuming I don't because I'm not explicitly retaining these objects anywhere in my code, and I don't synthesize these into getter/setters. But I don't know enough about nib unarchiving to know whether I should be releasing these in my dealloc phase or not?

推荐答案

这对iPhone来说是正确的;但是,它在Mac上是不正确的。

This is correct for the iPhone; it would not be correct on the Mac, though.

但是,您可能需要重新编写此代码。假设视图将是从笔尖加载的最后一个对象是不安全的。我建议您将它连接到视图控制器中的注释插座,或者扫描列表以查找作为Note子类的对象。 (如果您正在加载多个Notes并使用outlet选项,请确保在加载另一个之前添加一个Note。)

However, you may want to rework this code. It isn't safe to assume that the view will be the last object loaded from the nib. I'd suggest instead that you either connect it to a "note" outlet in your view controller or scan the list for an object that's a subclass of Note. (If you're loading multiple Notes and you use the outlet option, just make sure you add one Note before loading another.)

这篇关于使用loadNibNamed:方法时是否需要释放IBOutlets?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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