BB.
BB.

Reputation: 719

Why I can't release the navigation controller of the view I added to the window

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self setWindow:[[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]];

    // Override point for customization after application launch.
    //self.window.backgroundColor = [UIColor whiteColor];

    // Create First view
    FirstViewController *firstViewCTL = [[FirstViewController alloc] init];

    // Create UINavigationController
    UINavigationController *navCTL = [[UINavigationController alloc] init];
    [[navCTL navigationBar] setBarStyle:UIBarStyleBlack];
    [navCTL pushViewController:firstViewCTL animated:NO];
    [firstViewCTL release];

    [[self window] addSubview:navCTL.view];
    [navCTL release];
    [[self window] makeKeyAndVisible];
    return YES;
}

I understand that adding subview (addSubview:) will retain the view to be aded. But why I can't now release my navigation controller (navCTL) that own the views that has already been retained

Upvotes: 0

Views: 217

Answers (1)

Tom Irving
Tom Irving

Reputation: 10069

-addSubview: retains the view, not the view controller.

You can use UIWindow's rootViewController property (iOS 4 and higher) to retain the view controller, it also saves you adding the view as a subview yourself.

...
[window setRootViewController:navCTL];
[navCTL release];
...

Upvotes: 2

Related Questions