Lochana Ragupathy
Lochana Ragupathy

Reputation: 4320

Adding a ViewControllers's view as subView

I am adding a ViewControllers view as a subView to another ViewController,

Example :

In FirstScreen viewcontroller i do this,

         [self.view addSubview:self.secondScreen.view]; 

And Once i remove it from FirstScreen i do this

         [self.secondScreen.view removeFromSuperView];
          self.secondScreen=nil;  

But While Adding the subView ViewDidLoad method is called but while removeFromSuperView ViewDidUnLoad is not called.

My Question

1) will all my objects in my secondScreen will get deallocated once i set the instance self.secondScreen to nil

2)Is it safe to do like this so that i wont get any leaks or memory warning

Upvotes: 2

Views: 1737

Answers (2)

iDev
iDev

Reputation: 23278

Assuming that your app supports from iOS 5.0 onwards, you need to add it as,

[self addChildViewController:self.secondScreen];
[self.view addSubview:self.secondScreen.view]; 

Similarly for removing you can use removeFromParentViewController and then remove from superview. Check apple documentation here.

ViewDidUnLoad is deprecated from iOS 6.0 onwards and will not get called. Check the documentation here.

Regarding your questions,

1) will all my objects in my secondScreen will get deallocated once i set the instance self.secondScreen to nil

Once you are done with self.secondScreen class, it will start releasing objects inside this class once you set it to nil. If you are using ARC, you dont have to worry much about releasing. OS will take care of those things.

2)Is it safe to do like this so that i wont get any leaks or memory warning

Yes, this is fine if you are using ARC. For non-ARC, you need to make sure that you have released all variables properly in this class. Make sure the retain/release are all balanced in that case.

Upvotes: 2

David van Driessche
David van Driessche

Reputation: 7046

ViewDidUnload isn't called when a view is removed from a ViewController, it is called when the view is unloaded from memory. The iOS documentation about this has the following caveat:

Called when the controller’s view is released from memory. (Deprecated in iOS 6.0. Views are no longer purged under low-memory conditions and so this method is never called.)

Note the "deprecated" and the fact that "this method is never called".

Upvotes: 0

Related Questions