ios开发ARC,IBOutlets之strong与weak
2013-09-22 15:12
381 查看
今天在写程序的时候,用IBOutlets连了一个自定义的控件,出现了问题,后面访问的时候,控件里有些subviews没有初始化好,取到的时候为nil, 程序里用了ARC, IBOutlets一连接上,就自动加了weak的定义,于是知道问题应该是出现在这个上面了,weak改成strong,
问题是解决了,但是对IBOutlets什么时候用strong,什么时候用weak还是没有一个清楚的认识,于是去apple doc上看了下资料,发现下面一段:
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).
英文不是太好,大致理解意思是说,在 ARC 中,一般outlet属性都推荐使用 weak, 而 File's Owner连接到 nib 的顶层对象应该使用 strong。通俗一点说就是,如果是你自定义的view,不是做为主视图的子视图直接显示,而是你自己实例化创建出来并加入主视图里的,那么你需要自己保留对象所有权,需要使用strong。
关于这一点,参考了念茜blog里的文章:http://blog.csdn.net/yiyaaixuexi/article/details/7864974
总结:
之所以有上面困惑,是对对象所有权问题的没理解透彻,找到问题的根源,才能拔云见日。
问题是解决了,但是对IBOutlets什么时候用strong,什么时候用weak还是没有一个清楚的认识,于是去apple doc上看了下资料,发现下面一段:
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;
英文不是太好,大致理解意思是说,在 ARC 中,一般outlet属性都推荐使用 weak, 而 File's Owner连接到 nib 的顶层对象应该使用 strong。通俗一点说就是,如果是你自定义的view,不是做为主视图的子视图直接显示,而是你自己实例化创建出来并加入主视图里的,那么你需要自己保留对象所有权,需要使用strong。
关于这一点,参考了念茜blog里的文章:http://blog.csdn.net/yiyaaixuexi/article/details/7864974
总结:
之所以有上面困惑,是对对象所有权问题的没理解透彻,找到问题的根源,才能拔云见日。
相关文章推荐
- ios开发ARC,IBOutlets之strong与weak
- ios开发ARC,IBOutlets之strong与weak
- ios开发ARC,IBOutlets之strong与weak
- ios开发ARC,IBOutlets之strong与weak
- 【iOS开发系列】XIB IBOutlets use strong or weak ?
- 【iOS开发系列】XIB IBOutlets use strong or weak ?
- iOS开发:strong属性与weak属性的区别
- iOS学习笔记——ARC两三事(strong、weak详解,ARC与非ARC混编)
- iOS5 ARC,IBOutlets 应该定义strong还是weak
- iOS开发中@property的属性weak nonatomic strong readonly等介绍
- iOS开发之详解@property属性(copy,strong,weak,nonatomic,readwrite等)
- iOS5 ARC,IBOutlets 应该定义strong还是weak
- [iOS]arc机制中strong和weak的形象理解
- iOS开发中@property的属性weak nonatomic strong readonly等介绍
- iOS开发-assign、retain、copy、strong、weak的区别
- ios arc __strong与__weak的一个小实例
- IOS开发(77)之iOS高级内存管理:比较__unsafe_unretain、__strong、__weak、__autoreleasing
- [转]iOS开发中@property的属性weak nonatomic strong readonly等介绍
- Should IBOutlets be strong or weak under ARC
- iOS OC内存管理、ARC、property属性、__strong、__weak、__block——iOS 编码复习(一)