My iOS app is navigation-based with the following structure:
@interface ViewControllerA : UIViewController
@property (strong,nonatomic) ViewControllerB *viewControllerB;
@property (strong,nonatomic) ViewControllerC *viewControllerC;
...
viewControllerB
and viewControllerC
get instantiated before ViewControllerA
's navigationController
pushes them.
In my understanding, everything that is retained in ViewControllerA
should be set to nil in ViewControllerA
's viewDidUnload
.
Should I do the same to child view controllers? Like this:
-(void)viewDidUnload
{
self.viewControllerB=nil;
self.viewControllerC=nil;
}
I found an issue when there was "received memory warning" initiated from viewControllerC
.
Afterward, viewDidUnload
of the parent view controller (i.e. viewControllerA
) was called, thereby setting nil to 'viewControllerB'. Unexpectedly, viewDidUnload of viewControllerB is also called. So I got "message sent to deallocated object" if I set nil to viewControllerB
's subviews (in viewControllerB
's viewDidUnload
).
Does it mean that I should not set nil to child view controllers? What is the best practice for memory management in this situation?
P.S. I use ARC.