IBOutlet是否需要成为财产&合成的? [英] Does an IBOutlet needs to be a property & synthesized?
问题描述
在大多数示例中,我看到以下IBOutlets设置:
In most examples I see the following setup of IBOutlets:
(Example A)
FooController.h:
@interface FooController : UIViewController {
UILabel *fooLabel;
}
@property (nonatomic, retain) IBOutlet UILabel *fooLabel;
@end
FooController.m:
@implementation FooController
@synthesize fooLabel;
@end
但是这个工作也没问题(通知:没有财产也没有合成):
But this works also fine (notice: no property and no synthesize):
(Example B)
FooController.h:
@interface FooController : UIViewController {
IBOutlet UILabel *fooLabel;
}
@end
FooController.m:
@implementation FooController
@end
在例B中定义IBOutlets是否有任何缺点?像内存泄漏?似乎工作正常,我更喜欢不将IBOutlets公开为公共属性,因为它们不是这样使用的,它们仅用于控制器实现。在没有真正需要的情况下将它定义在三个地方并不会让我觉得非常干(不要重复自己)。
Are there any downsides of defining IBOutlets as in Example B? Like memory leaks? Seems to work fine and I prefer to not expose the IBOutlets as public properties as they are not used as such, they are only used in the controller implementation. Defining it in three places without a real need does not strike me as very DRY (Don't Repeat Yourself).
推荐答案
On Mac OS X,IBOutlets连接如下:
On Mac OS X, IBOutlets are connected like this:
- 查找名为set< OutletName>的方法:。如果存在则调用它。
- 如果不存在方法,请查找名为< OutletName>的实例变量,将其设置为而不保留。
- Look for a method called set<OutletName>:. If it exists call it.
- If no method exists, look for an instance variable named <OutletName>, set it without retaining.
在iPhone OS上,IBOutlets连接如下:
On iPhone OS, IBOutlets are connected like this:
- 调用[object setValue:outletValue forKey:@< OutletName>]
密钥设置值的行为是做某事像这样:
The behavior of set value for key is to do something like this:
- 寻找名为set< OutletName>的方法:。如果存在则调用它。
- 如果不存在方法,请查找名为< OutletName>的实例变量,设置它并保留。
- Look for a method called set<OutletName>:. If it exists call it.
- If no method exists, look for an instance variable named <OutletName>, set it and retain it.
如果您使用某个属性,则会进入查找名为set< OutletName>的方法:... 两个平台上的案例。如果您只使用实例变量,那么您将在Mac OS X VS iPhone OS上具有不同的保留/释放行为。使用实例变量没有任何问题,您只需要在平台之间切换时处理这种行为差异。
If you use a property, you'll fall into the "Look for a method called set<OutletName>:..." case on both platforms. If you just use an instance variable, then you'll have different retain/release behavior on Mac OS X VS iPhone OS. There's nothing wrong with using an instance variable, you just need to deal with this difference in behavior as you switch between platforms.
以下是关于此主题的完整文档的链接。
http://developer.apple.com/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html#//apple_ref/doc/uid/10000051i-CH4-SW6
Here's a link to full documentation on just this topic. http://developer.apple.com/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html#//apple_ref/doc/uid/10000051i-CH4-SW6
这篇关于IBOutlet是否需要成为财产&合成的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!