首先有一點(diǎn)剥哑,在OC中,如果對(duì)象沒(méi)有強(qiáng)引用淹父,就會(huì)被自動(dòng)釋放星持,那么為什么控件還可以設(shè)為weak?
1. 從storyboard或者xib上創(chuàng)建控件,在控件放在view上的時(shí)候弹灭,已經(jīng)形成了如下的引用關(guān)系,以UIButton為例:
UIViewController->UIView->subView->UIButton
然后你為這個(gè)UIButton聲明一個(gè)weak屬性
@property(nonatomic,weak) IBOOutlet UIButton *btn;
相當(dāng)于xib/sb對(duì)這個(gè)Button是強(qiáng)引用督暂,你聲明的屬性對(duì)它是弱引用揪垄。
2.手動(dòng)創(chuàng)建控件
a). 將控件聲明成strong
@property(nonatomic,strong) UIButton *btn;
那么你在實(shí)現(xiàn)這個(gè)控件時(shí)只需這樣:
_btn = [[UIButton alloc]init];
[self.view addSubview:_btn]
b). 將控件聲明成weak
@property(nonatomic,weak) UIButton *btn;
那么你在實(shí)現(xiàn)這個(gè)控件時(shí)需要這樣:
UIButton *button = [[UIButton alloc]init];
_btn = button;
[self.view addSubview:_btn];
============================
最近看的黑馬iOS視頻上給的建議的是:
1.如果用Stroyboard拖線,用weak
2.如果自定對(duì)象逻翁,用strong(但我還是習(xí)慣用weak暫時(shí)=_=)
其實(shí)不管聲明的屬性是強(qiáng)引用還是弱引用饥努,在控制器消失的時(shí)候,這個(gè)屬性消失八回,View消失酷愧,subViews消失,控件也就消失了缠诅。
=============================
之前專(zhuān)門(mén)搜過(guò)相關(guān)的問(wèn)題溶浴,貼上來(lái):
IBOutlet的屬性一般可以設(shè)為weak是因?yàn)樗呀?jīng)被view引用了,除非view被釋放管引,否則IBOutlet的屬性也不會(huì)被釋放士败,另外IBOutlet屬性的生命周期和view應(yīng)該是一致的,所以IBOutlet屬性一般設(shè)為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).
簡(jiǎn)單的說(shuō)谅将,如果IBOutlet對(duì)象是nib/sb scene的擁有者(File’s owner)所持有的對(duì)象,那么很顯然擁有者必須“擁有”對(duì)象的指針重慢,因此屬性應(yīng)設(shè)置為strong饥臂。而其他的IBOutlet對(duì)象的屬性需要設(shè)置為weak,因?yàn)閾碛姓卟⒉恍枰皳碛小彼麄兊闹羔標契狻Ee例來(lái)說(shuō)隅熙,UIViewController的view屬性是strong,因?yàn)閏ontroller要直接擁有view核芽。而添加到view上的subviews猛们,作為IBOutlet只需要設(shè)置為weak就可以了,因?yàn)樗麄儾皇莄ontroller直接擁有的狞洋。直接擁有subviews的是controller的view弯淘,ARC會(huì)幫助管理內(nèi)存。
緊接著吉懊,文檔里又提到:
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.
第一種情形前面已經(jīng)解釋過(guò)了庐橙,對(duì)于第二種,通俗點(diǎn)將借嗽,就是controller需要直接控制某一個(gè)subview并且將subview添加到其他的view tree上去态鳖。
單純從ARC的角度思考,用weak也是很顯然的:因?yàn)閟ubview添加到view上時(shí)恶导,view會(huì)“擁有”subview浆竭。當(dāng)然,給IBOutlet屬性設(shè)置為strong也沒(méi)有錯(cuò),“糾結(jié)誰(shuí)對(duì)誰(shuí)錯(cuò)“的問(wèn)題可能需要上升到模式或者編碼習(xí)慣的問(wèn)題邦泄,已經(jīng)超出本文的范圍删窒。