在 @interface 内部还是外部声明 IBOutlet?
抱歉,如果我对这个太挑剔了,但我现在正在学习 iOS 编程,我发现有些人像这样声明 IBOutlet :
IBOutlet 附加到属性
#import <UIKit/UIKit.h>
#import "CustomCell.h"
@interface CustomTableViewController : UITableViewController {
CustomCell *customCell;
}
@property (nonatomic, retain) IBOutlet CustomCell *customCell;
@end
还有一些人这样声明:
IBOutlet 附加对于接口内部的声明,
#import <UIKit/UIKit.h>
#import "CustomCell.h"
@interface CustomTableViewController : UITableViewController {
IBOutlet CustomCell *customCell;
}
@property (nonatomic, retain) CustomCell *customCell;
@end
哪一种是声明它的正确方法?它们之间有什么区别吗? 如果有人知道解释为什么他们把它放在不同的地方,那么学习就会很棒。
多谢 :)
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:
IBOutlet attached to property
#import <UIKit/UIKit.h>
#import "CustomCell.h"
@interface CustomTableViewController : UITableViewController {
CustomCell *customCell;
}
@property (nonatomic, retain) IBOutlet CustomCell *customCell;
@end
And some declaring like this:
IBOutlet attached to the declaration inside the interface
#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.
Thanks a lot :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这两个仍然是“界面内部”,所以你的标题有点令人困惑,但我明白你在问什么。
在许多情况下,这两种方法的结果是相同的,但又有所不同。 IBOutlet 属性将调用该属性的 setter 方法,如果设置该属性应该产生一些副作用,则该方法使您有机会覆盖该 setter。
我更喜欢在属性上使用出口,因为我认为这使得从 nib 加载的对象的内存管理更加清晰。看看 nib 对象的内存管理,我想你会明白我的意思。
IBOutlet ivars 将调用这些 ivars(如果存在)的 setter,如果没有找到 setter,则直接保留从 nib 加载的对象。
将属性宣传为 IBOutlet 至少可以清楚地表明,该属性的 setter 将始终被使用,并遵循为该属性设置的任何内存管理规则。
最后,我认为 IBOutlet 是类的公共接口的一部分,因此最好公开方法(通过属性)来急切地使用它们,而不是使用 -setValue:forKey: 来操作支持 ivars,这应该是一个实现细节。
Both of those are still "inside the interface" so your title it a bit confusing but I see what you are asking.
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.
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.
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.
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.
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.
这两种样式是可以互换的,生成的代码或从笔尖加载对象的方式没有区别。真的。
然而,两种样式都有多余的线条。只需省略 ivar 声明即可。 仅仅一行
在现代运行时,
就足够了。如果您有一个复杂的项目,我建议将所有出口从公共接口移至单独的头文件中。大多数插座都是私有接口,将它们放在标头中的唯一原因是 Interface Builder 可以找到它们。
The two styles are interchangeable, there is no difference in the generated code or the way objects will be loaded from a nib. Really.
However, both styles have a redundant line. Simply leave out the ivar declaration. Just the line
is sufficient in the modern runtime.
If you have a complex project, I suggest moving all the outlets out of the public interface into a separate header file. Most outlets are private interface, the only reason to have them in a header is so Interface Builder can find them.
两种方式都可以声明,实际上没有区别。
但是,事情是这样的:
如果你需要你的类有一些具有特殊行为的 ivar 或者它必须从外部访问等,并且它必须是一个属性,那么我会说您有 2 个选项可供选择(附加到属性和类接口内部)。
如果不是您的情况,请不要创建属性,这不是必需的,只需在类接口内执行即可。
希望它有帮助;)
You can declare both ways, there is no difference actually.
But, here is the thing:
If you need your class to have some ivar with a special behavior or it has to be accessed from outside, etc, and it has to be a property, then I will say you have 2 options to choose from (attached to the property and inside the class interface).
If that is not your case, don't create a property, is not necessary, just do it inside your class interface.
Hope it helps ;)