views:

409

answers:

2

I'm new to iPhone development and am having problems removing a sub-view from the main window. The problem is that the view still shows up even after calling removeFromSuperview.

The sub-view is created and added to the display tree through this code:

// Instantiate the controller for the authentication view
AuthenticationController* controller = [AuthenticationController alloc];
[controller initWithNibName:@"AuthenticationView" bundle:[NSBundle mainBundle]];
authController = controller;

// Add the authentication view to the window
[[stateManager appWindow] addSubview:[authController view]];

Then later, and I have verified that this code is run by setting a breakpoint, this is how I'm attempting to remove the view:

[[authController view] removeFromSuperview];

In case it matters, here's the dealloc code that does the for the owner of the view controller:

- (void)dealloc {
    [authController release];
    [super dealloc];
}

What is causing this sub-view to continue to show up?

A: 

I got this working. Apparently, a view doesn't go away until it's deallocated, and I had a misunderstanding of how memory management works on this platform. Here's my corrected code:

AuthenticationController* controller = [[AuthenticationController alloc]
initWithNibName:@"AuthenticationView" bundle:[NSBundle mainBundle]];
controller.delegate = self;
authController = controller;
[controller release]; // <-- Problem was that a reference was being maintained
[[stateManager appWindow] addSubview:[authController view]];
Jacob
This doesn't look like it should work. The `initWithNibName:` method spawns a controller with a retain count of 1. You assign it `authController` (which I assume is an instance variable) without retaining it. Then release it setting the retain count back to zero and the object should be deallocated. When you get to the last line, `authController` should be gone.
Squeegy
I don't think your assessment is correct. `authController` is a property that retains a reference. So before I had `[controller release]`, the object had a retain count of 2, and the `release` during my `dealloc` therefore wasn't freeing the object.
Jacob
A: 

Not sure what you mean by "show up." On screen? In memory?

Your "fix" looks buggy, in that alloc gives you one reference, you then release it, which gets rid of the AuthenticationController. Which you then use.

This might appear to work since there hasn't been anyone overwriting the controller before you read its view, but this is just asking for trouble.

David Dunham
I meant show up on screen.
Jacob
I don't think your assessment is correct. In my code, `authController` is a property that retains a reference. So before I had `[controller release]`, the object had a retain count of 2. Therefore, the `release` during my `dealloc` wasn't freeing the object.
Jacob
Well, we don't have ESP, and that makes two of us who thought it was an instance variable (since you were using it as one, with the release).If it's a property, you set it to nil rather than releasing.
David Dunham