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 af开发者_如何学Pythonter 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?
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]];
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.
精彩评论