控件的strong和weak问题
来源:互联网 发布:英雄无敌3恐怖骑士知乎 编辑:程序博客网 时间:2024/06/06 13:21
首先有一点,在OC中,如果对象没有强引用,就会被自动释放,那么为什么控件还可以设为weak?
1. 从storyboard或者xib上创建控件,在控件放在view上的时候,已经形成了如下的引用关系,以UIButton为例:
UIViewController->UIView->subView->UIButton
然后你为这个UIButton声明一个weak属性
相当于xib/sb对这个Button是强引用,你声明的属性对它是弱引用。
2.手动创建控件
a). 将控件声明成strong
那么你在实现这个控件时只需这样:
b). 将控件声明成weak
那么你在实现这个控件时需要这样:
============================
之前看的黑马iOS视频上给的建议的是:
1.如果用Stroyboard拖线,用weak
2.如果自定对象,用strong
<img src="https://pic1.zhimg.com/3d228aa8fc004971843129deee88f490_b.jpg" data-rawwidth="972" data-rawheight="724" class="origin_image zh-lightbox-thumb" width="972" data-original="https://pic1.zhimg.com/3d228aa8fc004971843129deee88f490_r.jpg">其实不管声明的属性是强引用还是弱引用,在控制器消失的时候,这个属性消失,View消失,subViews消失,控件也就消失了。
=============================
之前专门搜过相关的问题,贴上来:
IBOutlet的属性一般可以设为weak是因为它已经被view引用了,除非view被释放,否则IBOutlet的属性也不会被释放,另外IBOutlet属性的生命周期和view应该是一致的,所以IBOutlet属性一般设为weak。
可参考如下:
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:
UIViewController->UIView->subView->UIButton
然后你为这个UIButton声明一个weak属性
@property(nonatomic,weak) IBOOutlet UIButton *btn;
2.手动创建控件
a). 将控件声明成strong
@property(nonatomic,strong) UIButton *btn;
_btn = [[UIButton alloc]init];[self.view addSubview:_btn]
b). 将控件声明成weak
@property(nonatomic,weak) UIButton *btn;
UIButton *button = [[UIButton alloc]init];_btn = button;[self.view addSubview:_btn];
============================
之前看的黑马iOS视频上给的建议的是:
1.如果用Stroyboard拖线,用weak
2.如果自定对象,用strong
<img src="https://pic1.zhimg.com/3d228aa8fc004971843129deee88f490_b.jpg" data-rawwidth="972" data-rawheight="724" class="origin_image zh-lightbox-thumb" width="972" data-original="https://pic1.zhimg.com/3d228aa8fc004971843129deee88f490_r.jpg">其实不管声明的属性是强引用还是弱引用,在控制器消失的时候,这个属性消失,View消失,subViews消失,控件也就消失了。
=============================
之前专门搜过相关的问题,贴上来:
IBOutlet的属性一般可以设为weak是因为它已经被view引用了,除非view被释放,否则IBOutlet的属性也不会被释放,另外IBOutlet属性的生命周期和view应该是一致的,所以IBOutlet属性一般设为weak。
可参考如下:
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).
简单的说,如果IBOutlet对象是nib/sb scene的拥有者(File’s owner)所持有的对象,那么很显然拥有者必须“拥有”对象的指针,因此属性应设置为strong。而其他的IBOutlet对象的属性需要设置为weak,因为拥有者并不需要“拥有”他们的指针。举例来说,UIViewController的view属性是strong,因为controller要直接拥有view。而添加到view上的subviews,作为IBOutlet只需要设置为weak就可以了,因为他们不是controller直接拥有的。直接拥有subviews的是controller的view,ARC会帮助管理内存。
紧接着,文档里又提到:
Outlets should be changed to strong when the outlet should be considered to own the referenced object:
- As indicated previously, this is often the case with File’s Owner—top level objects in a nib file are frequently considered to be owned by the File’s Owner.
- You may in some situations need an object from a nib file to exist outside of its original container. For example, you might have an outlet for a view that can be temporarily removed from its initial view hierarchy and must therefore be maintained independently.
第一种情形前面已经解释过了,对于第二种,通俗点将,就是controller需要直接控制某一个subview并且将subview添加到其他的view tree上去。
单纯从ARC的角度思考,用weak也是很显然的:因为subview添加到view上时,view会“拥有”subview。当然,给IBOutlet属性设置为strong也没有错,“纠结谁对谁错“的问题可能需要上升到模式或者编码习惯的问题,已经超出本文的范围。
0 0
- 控件的strong和weak问题
- 关于声明控件用weak和strong的问题&NSString用copy好还是strong
- weak,strong引发的问题
- strong 和weak的理解
- weak 和 strong 的区别
- strong和weak的区别
- strong 和 weak 的区别
- strong和weak的区别
- strong和weak的区别
- 87-tableView的注册机制 ,strong和weak问题
- 在ios开发中weak和strong的使用问题
- weak/strong创建控件的区别
- UI控件strong,weak指针的选择
- iOS中控件的strong与weak
- Object-C strong和weak的解释
- iOS5中strong和weak的理解
- iOS strong 和weak的理解
- iOS strong 和weak的形象理解
- iOS中的设计模式
- Shell 变量和选项
- 新的开始,最后的开始
- 1.3 pbrt:系统综述(1.3.1-1.3.3)
- 输入参数总结
- 控件的strong和weak问题
- LWPCookieJar的使用
- INS-06006 Passwordless SSH connectivity not set up between the following node
- Caffe —— Deep learning in Practice
- 在laravel框架中如何查看自己写的sql原生格式
- poi get cell value is formula
- Topcoder SRM 616 Div2 1000 TwoLLogo
- nova-authconsole HA方案memcached配置
- OC 方法,继承,多态 练习题