为什么removeFromSuperview不能在我的iPhone应用程序中隐藏我的UIView?

时间:2010-01-29 17:50:21

标签: iphone objective-c cocoa-touch uiview

我是iPhone开发的新手,在从主窗口中删除子视图时遇到问题。问题是即使在调用removeFromSuperview之后视图仍然显示。

创建子视图并通过以下代码添加到显示树:

// 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]];

然后,我已经通过设置断点验证了此代码是运行的,这就是我试图删除视图的方式:

[[authController view] removeFromSuperview];

如果重要,这里是为视图控制器的所有者执行的dealloc代码:

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

是什么导致此子视图继续显示?

2 个答案:

答案 0 :(得分:0)

我得到了这个工作。显然,一个视图在被解除分配之前不会消失,我对内存管理在这个平台上的工作方式产生了误解。这是我更正后的代码:

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]];

答案 1 :(得分:-1)

不确定你的意思是“出现”。在屏幕上?记忆中?

您的“修复”看起来有些错误,因为alloc会为您提供一个引用,然后释放它,它将摆脱AuthenticationController。然后你使用它。

这似乎可行,因为在您阅读其视图之前没有人覆盖控制器,但这只是在寻找麻烦。