您的位置:首页 > 其它

Should IBOutlets be strong or weak under ARC?

2013-11-18 15:50 387 查看
I am developing exclusively for iOS 5 using ARC. Should
IBOutlet
s
to
UIView
s
(and subclasses) be
strong
or
weak
?

The following:
@property (nonatomic, weak) IBOutlet UIButton *button;


Would get rid of all of this:
- (void)viewDidUnload
{
// ...
self.button = nil;
// ...
}


Are there any problems doing this? The templates are using
strong
as
are the automatically generated properties created when connecting directly to the header from the 'Interface Builder' editor, but why? The
UIViewController
already
has a
strong
reference
to its
view
which
retains its subviews.


Answers

Summarized from the developer
library:

From a practical perspective, in iOS and OS X outlets should be defined as declared properties. Outlets should generally be weak, except for those from File’s Owner to top-level objects in a nib file (or, in iOS, a storyboard scene) which should be strong.
Outlets that you create will therefore typically be weak by default, because:

Outlets that you create to, for example, subviews of a view controller’s view or a window controller’s window, are arbitrary references between objects that do not imply ownership.

The strong outlets are frequently specified by framework classes (for example, UIViewController’s view outlet, or NSWindowController’s window outlet).
@property (weak) IBOutlet MyView *viewContainerSubview;
@property (strong) IBOutlet MyOtherClass *topLevelObject;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: