views:

524

answers:

3

I'm trying to load a new view into an existing view controller, but I want to load that view from a xib file. My plan was to create a second viewController (viewController1 in the code below), then retain its view and release that viewController that I just created. I was hoping that the viewController would be released and the view would stick around, but that doesn't seem to be happening.

Question 1: If a viewcontroller gets dealloced, does its associated view get dealloced no matter what the view's retain count is? In my sample code below, you can see that the view has a retain count of 13 before it suddenly just disappears.

Question 2: Why does retaining the view increase its retainCount by 3?

PageViewController *viewController1 = [[PageViewController alloc] initWithNibName:@"Page1" bundle:nil];
[viewController1.view setUserInteractionEnabled:YES];

NSLog (@"vc retain count: %d", [viewController1 retainCount]); //count=1
NSLog (@"vc view retain count: %d", [viewController1.view retainCount]); //count=4

self.currentPageView=viewController1.view;

NSLog (@"vc retain count: %d", [viewController1 retainCount]); //count=1
NSLog (@"vc view retain count: %d", [viewController1.view retainCount]); //count=7


[viewController1.view retain];

NSLog (@"vc retain count: %d", [viewController1 retainCount]); //count=1
NSLog (@"vc view retain count: %d", [viewController1.view retainCount]); //count=10

[self.view addSubview:viewController1.view];

NSLog (@"vc retain count: %d", [viewController1 retainCount]); //count=1
NSLog (@"vc view retain count: %d", [viewController1.view retainCount]); //count=13

[viewController1 release];

NSLog (@"vc view retain count: %d", [viewController1.view retainCount]); 
//objc[3237]: FREED(id): message view sent to freed object=0x538ce0
+1  A: 

The error you're getting about the "message sent to freed object" isn't telling you that the view has been freed, it's that viewController1 has been freed, and thus you're getting an error when you send it the "view" message. (remember that in Objective C every property access really sends a message...)

I'm not sure why the view's retain count is jumping up by 3 each time, though.

David Maymudes
Thanks, I still haven't figured out why the retain count is jumping up by 3 each time, either, but I think this is messy enough that I'm just going to try refactoring things to avoid the problem entirely. I wish I had enough reputation to vote up Hikaru's answer below, also, it was helpful as well.
niels
+1  A: 

This might help:

[[NSBundle mainBundle] loadNibNamed:@"Page1" owner:self options:nil];

where self is the existing viewController.

nico
+1  A: 

this line is pointless

self.currentPageView=viewController1.view;

The view in viewController1 is not yet built because the method loadView in that controller is not called

although you can add new subview into the viewController.view because of "magic" allowing you add an object to a view that is not built yet.

It doesn't change the fact - the viewController.view does not exist at that time.

note: all the controller.view is built in viewDidLoad/loadView method, and viewDidLoad/loadView will not call until it's going to display(eg. pushController)

usually i don't rely on retain counter because it's not reliable.

Hikaru