Playing around in Instruments, I noticed something I don’t understand. I create a couple of UIViewControllers and add their views to another UIView:
CustomVC *vc = [[CustomVC alloc] initWithCustomInitializer:someParameter];
[mainView addSubview:vc.view];
By logging the memory address of the CustomVC in its init and dealloc, I see that vc is deallocated almost immediately, though the view remains on screen and everything works fine.
Does the UIView not necessarily need its controller? Or is something else going on that I’m misinterpreting?
A
UIViewdoesn’t/shouldn’t retain its parent (theUIViewController), so if you drop your reference to theUIViewControllerand only keep the reference to theUIView, nothing keeps a strong reference to it and it will be released.The weak reference in the
UIViewto its controller will be automatically set to nil.Whether the
UIViewneeds its “lost” controller to do its work is another story though, and entirely depending on theUIView.