I try to find proper way of releasing modal view controller.
Basically, I have view controller, which presents modal view (fullscreen) after button is pressed.
TipViewController * tipViewController = [[TipViewController alloc] init];
tipViewController.delegate = self;
[self presentModalViewController:tipViewController animated:YES];
Then, in modal view when it should be dissmissed I call:
[self.delegate didDismissModalView];
Finally, the didDissmissModalView method of parent controller is following:
- (void)didDismissModalView
{
// dismiss the modal view controller
[self dismissModalViewControllerAnimated:YES];
}
(I use ModalViewControllerDelegate protocol, which requires to implement that method).
First I thought that I should release tipViewController in dealloc method of parent controller:
- (void)dealloc
{
[tipViewController release];
}
But then I saw, that it could be wrong way, because the modal view controller may be presented and dismissed many times before parent controller will be closed, and every time it would be allocated but only once released eventually.
So maybe I should release tipViewController just after presenting it?
TipViewController * tipViewController = [[TipViewController alloc] init];
tipViewController.delegate = self;
[self presentModalViewController:tipViewController animated:YES];
[tipViewController release];
Can I do this, althought the modal view is now displayed?
Or maybe I should release modal view that way:
- (void)didDismissModalView
{
// dismiss the modal view controller
[self dismissModalViewControllerAnimated:YES];
[self.modalViewController release];
}
assuming that self.modalViewController is the same as tipViewController now?
See Question&Answers more detail:
os