tags:

views:

30

answers:

1

Consider the following code in the initialiser of a class:

UIViewController* blankViewController=[[DisplayViewController alloc] 
                   initWithNibName:@"Blank" bundle:nil];
self.nextView=blankViewController.view;

nextView is a property that uses retain. Notice that blankViewController was not released. If it were released, this would cause a crash as the view doesn't seem to keep a reference to the viewController. I want the view controller to stick around as long as a reference to the view is kept. What is the nicest way to fix this memory leak?

+2  A: 

You need to store blankViewController as an ivar of self, or make it a (static) global variable.

Then, when self is deallocated, you call [blankViewController release].

You need to do this because there's no other (documented) way to get the view controller from a view.

KennyTM