iOS中控件的strong与weak
来源:互联网 发布:数控车床有编程软件吗 编辑:程序博客网 时间:2024/05/31 13:15
1. 从storyboard或者xib上创建控件,在控件放在view上的时候,已经形成了如下的引用关系,以UIButton为例:
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];
说明:为什么这里申明成weak可以。因为
[self.view addSubview:_btn]往数组中添加元素的时候会对元素进行retain,所以只要不执行removefromsuperview对象会一直持有button。
=============================之前专门搜过相关的问题,贴上来: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也没有错,“纠结谁对谁错“的问题可能需要上升到模式或者编码习惯的问题,已经超出本文的范围。
1 0
- iOS中控件的strong与weak
- 在IOS开发中控件的weak与strong的分析
- iOS中assign与weak,retain与strong的区别
- iOS中strong和weak的区别
- UI控件strong与weak
- 为什么 iOS 开发中,控件一般为 weak 而不是 strong?
- IOS开发(87)之Strong与Weak的理解
- iOS基础:strong属性与weak属性的区别
- strong与weak的理解
- strong与weak的区别
- [iOS]arc机制中strong和weak的形象理解
- iOS中strong&weak和_block&_weak的作用比较
- 在ios开发中weak和strong的使用问题
- iOS开发中@property的属性weak nonatomic strong readonly
- iOS开发中@property的属性weak nonatomic strong readonly
- ios中strong和weak的解释理解
- iOS:strong 与weak何时使用
- iOS - property中的strong与weak
- hdu 1061 Rightmost Digit 快速幂取模
- c/c++在windows下获取时间和计算时间差的几种方法总结
- 常见算法
- 经典搜索(深搜+剪枝+Java细节注意)
- raspberry-pi之DSI
- iOS中控件的strong与weak
- 【HDU 2457】 【POJ 3691】 DNA repair AC自动机+DP;
- 后序式的计算程序?
- Android开发笔记(七十三)代码混淆与反破解
- UVA 11549(p42)----Calculator Conundrum
- 关联规则
- C#单例模式
- 最大子数组求法--动态规划
- 用opencv自带的Haar分类器进行人脸检测(一)