Why doesn't initWithRootViewController retain the viewController Class that is passed into it?

StackOverflow https://stackoverflow.com/questions/1054993

  •  20-08-2019
  •  | 
  •  

Question

I have a custom viewController called SourceListViewController, and I'm adding it into a UINavigationController, the view of which is then added to the window of the iphone App. After passing the SourceListViewController to UINavigationController, I release the sourceListViewController.

SourceListViewController *sourceListVC = [[SourceListViewController alloc] initWithNibName:@"SourceListViewController" bundle:nil];
UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:sourceListVC] autorelease];
[sourceListVC release];

When I do this, the app would crash after the view is loaded onto the phone. When I commented out the last line, the app work fine. Isn't initWithRootViewController supposed to retain the copy of sourceListVC?

Was it helpful?

Solution

You are autoreleasing navigationController. So if navigationController gets autoreleased (which will probably happen in the next runloop) then so will sourceListVC.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top