我正在使用ARC专门为iOS 5开发.应该IBOutlets UIView(和子类)是strong或weak?
下列:
@property (nonatomic, weak) IBOutlet UIButton *button;
Run Code Online (Sandbox Code Playgroud)
将摆脱所有这一切:
- (void)viewDidUnload
{
// ...
self.button = nil;
// ...
}
Run Code Online (Sandbox Code Playgroud)
这样做有什么问题吗?模板正在使用strong,当从"Interface Builder"编辑器直接连接到标题时创建的自动生成属性,但为什么?在UIViewController已经有一个strong到其基准view保留其子视图.
cocoa-touch objective-c interface-builder ios automatic-ref-counting
在大多数示例中,我看到以下IBOutlets设置:
(Example A)
FooController.h:
@interface FooController : UIViewController {
UILabel *fooLabel;
}
@property (nonatomic, retain) IBOutlet UILabel *fooLabel;
@end
FooController.m:
@implementation FooController
@synthesize fooLabel;
@end
Run Code Online (Sandbox Code Playgroud)
但这也很好(注意:没有属性也没有合成):
(Example B)
FooController.h:
@interface FooController : UIViewController {
IBOutlet UILabel *fooLabel;
}
@end
FooController.m:
@implementation FooController
@end
Run Code Online (Sandbox Code Playgroud)
如例B中那样定义IBOutlets是否有任何缺点?像内存泄漏?似乎工作正常,我更喜欢不将IBOutlets公开为公共属性,因为它们不是这样使用的,它们仅用于控制器实现.在没有真正需要的情况下将它定义在三个地方并不会让我觉得非常干(不要重复自己).