隐藏不同视图控制器的子视图

发布于 2024-10-04 20:05:02 字数 1186 浏览 3 评论 0原文

我有一个充当另一个自定义警报视图的背景(浅灰色 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

成熟稳重的好男人 2024-10-11 20:05:02

您似乎没有删除视图,您只是通过将 alpha 设置为零来使视图不可见。因此,每次调用第二个代码示例时,您都会向 self.view 添加新版本的背景视图和对话框视图。在第二次调用时,您将有两个背景视图,都带有 tag = 1,并且您将从调用 [self.view.superview viewWithTag:1] 中获取第一个背景视图,这就是为什么您新添加的背景视图不会变得不可见。

但这还不是全部,ResultDialogControllerResultBackgroundViewController 还存在内存泄漏。当您调用 initWithNibName:bundle: 时,不需要调用 retain。也许您这样做是因为释放控制器时发生了崩溃?

您应该做的是为您的控制器创建 ivars 和属性。

@property (nonatomic, retain) ResultDialogController *resultController;
@property (nonatomic, retain) ResultBackgroundController *backgroundController;

然后,当显示控制器时,您可以执行以下操作:

ResultDialogController *dialogController = [[ResultDialogController alloc] initWithNibName:@"ResultDialogController_" bundle:nil];
self.dialogController = dialogController;

ResultBackgroundViewController *bgViewController = [[ResultBackgroundViewController alloc] initWithNibName:@"ResultView" bundle:nil];
self.backgroundController = bgViewController;

// do the same as before

然后在 buttonTapped: 中执行:

[UIView animateWithDuration:0.5f
     animations: ^{
      self.dialogController.view.alpha = 0;
      self.backgroundController.view.alpha = 0;
     }
     completion: ^(BOOL finished){
      [self.dialogController.view removeFromSuperview];
      [self.backgroundController.view removeFromSuperview]; 
     }
 ];

最重要的是,不要忘记在 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 and ResultBackgroundViewController. The call to retain is not necessary when you are calling initWithNibName: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.

@property (nonatomic, retain) ResultDialogController *resultController;
@property (nonatomic, retain) ResultBackgroundController *backgroundController;

Then when showing the controllers you can do something like:

ResultDialogController *dialogController = [[ResultDialogController alloc] initWithNibName:@"ResultDialogController_" bundle:nil];
self.dialogController = dialogController;

ResultBackgroundViewController *bgViewController = [[ResultBackgroundViewController alloc] initWithNibName:@"ResultView" bundle:nil];
self.backgroundController = bgViewController;

// do the same as before

Then in buttonTapped: you do:

[UIView animateWithDuration:0.5f
     animations: ^{
      self.dialogController.view.alpha = 0;
      self.backgroundController.view.alpha = 0;
     }
     completion: ^(BOOL finished){
      [self.dialogController.view removeFromSuperview];
      [self.backgroundController.view removeFromSuperview]; 
     }
 ];

And to top it off, don't forget to release the controller ivars in dealloc.

九厘米的零° 2024-10-11 20:05:02

您可以通过将视图的 HIDE 属性设置为 true 来隐藏它们。

You can hide them by setting HIDE property for the views to be true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文