当在ARC下取消分配视图控制器时,如果另一个对象具有强引用,是否还会取消分配其属性? [英] When a view controller is dealloc'd under ARC, are it's properties also dealloc'd if another object has a strong reference?
问题描述
我有一个地图应用程序,它使用外围滑入/滑出右视图控制器,就像下面显示的Google Maps应用程序(
I have a mapping application that uses a peripheral slide in/slide out right hand view controller, much like the Google Maps application shown below (source):
在我的应用中,此滑出视图具有weak
delegate
属性,我将地图视图控制器设置为委托(例如,在myMapViewController
内部的mapOptionsViewController.delegate = self
).我也希望使用右手幻灯片查看其他类型的信息.例如.在地图上显示用户选择的餐厅菜单menuViewController
.
In my app, this slide out view has a weak
delegate
property, and I set the map view controller to be the delegate (e.g. mapOptionsViewController.delegate = self
from inside myMapViewController
). I also wish to use the right hand slide in view for other sorts of information though. E.g. displaying a menu for a restaurant that a user selects on the map, menuViewController
.
我的问题是,如果我将mapOptionsViewController
换成menuViewController
,是否还需要从myMapViewController
手动nil
出mapOptionsViewController.delegate
吗?我担心的是,即使在我切换到menuViewController
时对mapOptionsViewController
进行了分配/处置,由于myMapViewController
仍指向旧的mapOptionsViewController.delegate
,所以mapOptionsViewController.delegate
的内存将不会被释放堆.
My question is, if I swap out mapOptionsViewController
for menuViewController
, do I need to manually nil
out the mapOptionsViewController.delegate
from myMapViewController
also? My concern is that even if mapOptionsViewController
is dealloc'd/disposed of when I do the switch to menuViewController
, because myMapViewController
still points to the old mapOptionsViewController.delegate
, mapOptionsViewController.delegate
's memory will not be released from the heap.
因此,基本上,如果您有一个对象视图控制器foo
(我的地图),它是另一个视图控制器bar
(地图选项)的委托,例如bar.delegate = foo
,那么您将bar
对于baz
(餐厅菜单),我是否需要从foo
设置bar.delegate = nil
.或者,当包含视图控制器delegate
时bar
取消delegate
会由于被baz
替换而被取消分配?
So basically, if you have an object view controller foo
(my map) which is the delegate for another view controller bar
(map options), such that bar.delegate = foo
, and then you switch out bar
for baz
(restaurant menu), do I need to set bar.delegate = nil
from foo
. Or, will bar
nil out delegate
when the view controller delegate
is contained in is dealloc'd as a result of being replaced with baz
?
推荐答案
总之,没有.只要一个对象仍然具有强引用,并且最后一个具有字符串引用的对象没有被释放,它就不会被释放.或者,您有一个错误.
In one word, no. as long as an object still have strong reference, and the last object that have a string reference to it is not deallocated, it is not deallocated. Or, you have a bug.
此外,当weak
引用的对象被释放时,该引用将自动清零.但是assign
不会.
Also, weak
references will be automatically zeroed when the object referencing to it is deallocated. However assign
ones will not.
这篇关于当在ARC下取消分配视图控制器时,如果另一个对象具有强引用,是否还会取消分配其属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!