iboutlet uibutton到底是weak还是strong
zhongshandaoren2022-10-04 11:39:541条回答
已提交,审核后显示!提交回复
共1条回复
- dznicbc 共回答了16个问题
|采纳率100% - 今天用Xcode5的时候,发现默认的IBoutlet的属性设置为weak——因为Xcode5建立的工程都是ARC的了.但是当时还有点不明白,因为项目的原因,一直没有正式使用过ARC.于是,为了搞清楚为什么,google了一下,有很多答案.试着从Apple文档寻找线索,在这里找到了说明:
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 should therefore typically be weak,because:
Outlets that you create to subviews of a view controller’s view or a window controller’s window,for example,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’swindow 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也没有错,“纠结谁对谁错“的问题可能需要上升到模式或者编码习惯的问题,已经超出本文的范围.
最后附上stakoverflow上的答案:
除了选中的答案之外,其他人的回答也非常有意思,大家可以看看. - 1年前
相关推荐
- iboutlet uibutton到底是weak还是strong
偶尔觉得1年前1
-
chengsj1209 共回答了24个问题
|采纳率91.7%今天用Xcode5的时候,发现默认的IBoutlet的属性设置为weak——因为Xcode5建立的工程都是ARC的了.但是当时还有点不明白,因为项目的原因,一直没有正式使用过ARC.于是,为了搞清楚为什么,google了一下,有很多答案.试着从Apple文档寻找线索,在这里找到了说明:
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 should therefore typically be weak,because:
Outlets that you create to subviews of a view controller’s view or a window controller’s window,for example,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’swindow 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也没有错,“纠结谁对谁错“的问题可能需要上升到模式或者编码习惯的问题,已经超出本文的范围.
最后附上stakoverflow上的答案:
除了选中的答案之外,其他人的回答也非常有意思,大家可以看看.1年前查看全部
大家在问
- 1负20、负0.04、负7分之52、负2又6分之1的倒数
- 2I have none of the knowledge she has
- 320fen哦,谁写的好就给谁①200÷(-3又3分之1)②(-0.4)÷0.02÷(-5分之1)
- 4(文科)点M是圆x 2 +y 2 =4上的一个动点,过点M作MD垂直于x轴,垂足为D,P为线段MD的中点.
- 56千分钟等于几小时
- 6看了下面这句广告语,你想到了什么?
- 7英语翻译粉彩山水薄胎碗尺寸:高 14CM 直径36CM 画工精美,工笔浑然有力.意境深远,高级工艺美术师所作,“珠山八友
- 8在追及问题中,为什么当两个物体的速度相等时,才会出现相距最远的情况?
- 9There are many trees and flowers _____ the garden.
- 10在直角三角形abc中 斜边ab上高的长是斜边ab长的四分之一,求它的两个锐角的大小
- 11怎样分清英语中表示食物的单词哪些是可数哪些是不可数?
- 12由甲苯和乙醛合成1-苯基-2-丙醇 以环己醇为原料合成OHCCH2CH2CH2CH2CHO
- 13扑克牌的13张黑桃分别是什么?
- 14His illness added much difficulty with our life.改错
- 15(2009•湖南)如图,当参数λ分别取λ1,λ2时,函数y=[x/1+λx](x≥0)的部份图象分别对应曲线C1和C2,