在@interface内部或外部声明IBOutlet? [英] Declaring IBOutlet inside or outside @interface?

查看:76
本文介绍了在@interface内部或外部声明IBOutlet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,如果我对这个问题过于挑剔,但我现在正在学习iOS编程,而且我似乎有些人宣称 IBOutlet 是这样的:

sorry If am I being too picky on this one but I am learning iOS programming now and I've seem some people who declare the IBOutlet like this:

#import <UIKit/UIKit.h>
#import "CustomCell.h"

@interface CustomTableViewController : UITableViewController {
  CustomCell *customCell;
}
@property (nonatomic, retain) IBOutlet CustomCell *customCell;
@end

有些声明如下:

#import <UIKit/UIKit.h>
#import "CustomCell.h"

@interface CustomTableViewController : UITableViewController {
  IBOutlet CustomCell *customCell;
}
@property (nonatomic, retain) CustomCell *customCell;
@end

哪一个是申报的正确方法?它们之间有什么区别吗?
如果有人知道为什么要把它放在不同的地方,那就太棒了。

which one is the proper way to declare it? Are any differences between them? If someone know to explain why do they put it on different places it would be awesome to learn.

非常感谢:)

推荐答案

这两个仍然在界面内所以你的标题有点令人困惑,但我看到你在问什么。

Both of those are still "inside the interface" so your title it a bit confusing but I see what you are asking.

在许多情况下,两种方法的结果都是相同的,但它们是不同的。 IBOutlet属性将调用属性的setter方法,如果设置该属性应该有一些副作用,它将为您提供覆盖该setter的机会。

In many cases the result of either approach will be the same but they are different. An IBOutlet property will call the property's setter method which gives you an opportunity to override that setter if setting that property should have some side effect.

我更喜欢在属性上使用outlet,因为我认为它使得从笔尖加载的对象的内存管理更加清晰。看看 nib对象的内存管理,我想你会明白我的意思。

I prefer to use outlets on properties because I think it makes the memory management of the objects loaded from the nib much clearer. Take a look at memory management of nib objects and I think you will see what I mean.


nib文件中的对象创建时保留计数为1,然后自动释放。在重建对象层次结构时,UIKit使用setValue:forKey:重新建立对象之间的连接,它使用可用的setter方法,或者如果没有setter方法可用,则默认保留对象。这意味着(假设您遵循奥特莱斯中显示的模式)您拥有插座的任何对象仍然有效。但是,如果有任何顶级对象未存储在出口中,则必须保留loadNibNamed:owner:options:方法返回的数组或数组内的对象,以防止这些对象过早释放。

Objects in the nib file are created with a retain count of 1 and then autoreleased. As it rebuilds the object hierarchy, UIKit reestablishes connections between the objects using setValue:forKey:, which uses the available setter method or retains the object by default if no setter method is available. This means that (assuming you follow the pattern shown in "Outlets") any object for which you have an outlet remains valid. 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.

IBOutlet ivars将调用那些ivars的setter(如果它们存在)并且如果没有找到setter则直接保留从nib加载的对象。

IBOutlet ivars will call setters for those ivars if they exists and directly retain the object loaded from the nib if no setter is found.

将属性作为IBOutlet广告,至少清楚地表明将始终使用属性的setter并遵循为该属性设置的任何内存管理规则。

Advertising the property as the IBOutlet at least makes it clear that the property's setter will always be used and follow whatever memory management rule has been set for that property.

最后我认为IBOutlets是类的公共接口的一部分,因此最好公开方法(通过属性)以便使用它们而不是使用 - setValue:forKey:操作支持ivars,这应该是一个实现细节。

Finally I argue that IBOutlets are part of the public interface of a class and it is therefore better to expose methods (via a property) for working with them eager than using -setValue:forKey: to manipulate the backing ivars which should be an implementation detail.

这篇关于在@interface内部或外部声明IBOutlet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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