I am using two view controllers. On button action of first view controller i am adding the second view controller’sview as subview to the first view controller. I am not able to release the second view controller so i am getting memory leak for the secondviewcontroller object. How to release the second view controller object after adding it as subview?
-(IBAction)buttonclick {
secondviewcontroller * obj = [[secondviewcontroller alloc]init];
[self.view addsubview:obj.view];
[obj release]; //if i release it here the view is also getting released and only black screen is displayed.how to release the second view controller obj after this?
}
sorry that was typing mistake earlier
You really shouldn’t be doing this.
In iPhone apps you should always present a single view controller for each screen.
I would suggest moving any logic you have in the second view controller into the backing class of the view you are adding.
If you’re dead set on having two view controllers, then hold a reference to the second view controller in the first view controller (retained) and release the second view controller whenever the second view is removed from its super view (and in the dealloc of the first view controller).