隐藏不同视图控制器的子视图
我有一个充当另一个自定义警报视图的背景(浅灰色 0.5 alpha)的视图。
当用户点击自定义警报上的“确定”按钮时,我也想隐藏自定义警报和背景视图。
两个视图都是同一个超级视图的子视图...
我在 buttonTapped:
方法中执行此操作来隐藏视图,并且它在第一次尝试时有效,但从第二次开始,背景视图永远不会关闭...警报每次都会正确隐藏。
[UIView animateWithDuration:0.5f animations:^{
self.view.alpha=0.0f; //hide alert
[self.view.superview viewWithTag:1].alpha=0.0f; //hide background
}];
它们被添加为子视图,如下所示:
ResultDialogController *dialogController = [[[ResultDialogController alloc] initWithNibName:@"ResultDialogController_" bundle:nil] retain];
ResultBackgroundViewController *bgViewController = [[[ResultBackgroundViewController alloc] initWithNibName:@"ResultView" bundle:nil] retain];
dialogController.view.alpha=0;
bgViewController.view.alpha=0;
bgViewController.view.tag=1;
[UIView animateWithDuration:0.5f animations:^{
bgViewController.view.alpha=0.5f;
dialogController.view.alpha=1.0f;
}];
[self.view addSubview:bgViewController.view];
[self.view addSubview:dialogController.view];
[dialogController release];
[bgViewController release];
如何始终关闭背景视图?
谢谢
I have a view that acts as a background (light grey 0.5 alpha) for for another custom alert view.
When the user taps my OK button on the custom alert, i want to hide the custom alert and the background view also.
Both views are subviews of the same superview...
I do this in the buttonTapped:
method to hide the views, and it works for the first attempt, but from the second time onwards, the background views never dismiss... the alerts hide correctly every time.
[UIView animateWithDuration:0.5f animations:^{
self.view.alpha=0.0f; //hide alert
[self.view.superview viewWithTag:1].alpha=0.0f; //hide background
}];
They are added as subviews, as follows:
ResultDialogController *dialogController = [[[ResultDialogController alloc] initWithNibName:@"ResultDialogController_" bundle:nil] retain];
ResultBackgroundViewController *bgViewController = [[[ResultBackgroundViewController alloc] initWithNibName:@"ResultView" bundle:nil] retain];
dialogController.view.alpha=0;
bgViewController.view.alpha=0;
bgViewController.view.tag=1;
[UIView animateWithDuration:0.5f animations:^{
bgViewController.view.alpha=0.5f;
dialogController.view.alpha=1.0f;
}];
[self.view addSubview:bgViewController.view];
[self.view addSubview:dialogController.view];
[dialogController release];
[bgViewController release];
How can i always dismiss the background view?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎没有删除视图,您只是通过将 alpha 设置为零来使视图不可见。因此,每次调用第二个代码示例时,您都会向
self.view
添加新版本的背景视图和对话框视图。在第二次调用时,您将有两个背景视图,都带有 tag = 1,并且您将从调用[self.view.superview viewWithTag:1]
中获取第一个背景视图,这就是为什么您新添加的背景视图不会变得不可见。但这还不是全部,
ResultDialogController
和ResultBackgroundViewController
还存在内存泄漏。当您调用initWithNibName:bundle:
时,不需要调用retain
。也许您这样做是因为释放控制器时发生了崩溃?您应该做的是为您的控制器创建 ivars 和属性。
然后,当显示控制器时,您可以执行以下操作:
然后在
buttonTapped:
中执行:最重要的是,不要忘记在 dealloc 中释放控制器 ivars。
You don't seem to remove the views, you are just making the invisible by setting the alpha to zero. So every time you call your second code sample you will add a new version of the background view and the dialog view to
self.view
. At the second call you will have two background views, both with tag = 1 and you are getting your first background view from the call to[self.view.superview viewWithTag:1]
which is why your newly added background view does not get invisible.But that is not all, you also have a memory leak for
ResultDialogController
andResultBackgroundViewController
. The call toretain
is not necessary when you are callinginitWithNibName:bundle:
. Perhaps you are doing this because you some crash when you released the controllers?What you should do is to create ivars and properties for your controllers.
Then when showing the controllers you can do something like:
Then in
buttonTapped:
you do:And to top it off, don't forget to release the controller ivars in dealloc.
您可以通过将视图的 HIDE 属性设置为 true 来隐藏它们。
You can hide them by setting HIDE property for the views to be true.