问题
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):
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.
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.
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?
回答1:
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?
So, let's see if I understand what you're doing.
You have a bar, with a weak reference to a foo, so bar.delegate = foo. You must also have something else (I'll call it a qux) with a strong reference to the same foo, because otherwise your foo would have disappeared. Let's say the relevant property is qux.menuThingy = foo.
You then do qux.menuThingy = baz. What happens to your bar?
If the qux was the only thing with a strong reference to the foo, there are now no more strong references to the foo, so the foo is deallocated. Because you used a weak reference, bar.delegate has been automatically set to nil for you. You don't have to do it yourself.
If however there was yet another object with a strong reference to the foo, then the foo is still around and hasn't been deallocated.
回答2:
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.
Also, weak references will be automatically zeroed when the object referencing to it is deallocated. However assign ones will not.
来源:https://stackoverflow.com/questions/16957151/when-a-view-controller-is-deallocd-under-arc-are-its-properties-also-dealloc