使用Grand Central Dispatch和UINavigationController的UI问题(UI issue using Grand Central Dispatch and UINav

编程入门 行业动态 更新时间:2024-10-28 07:25:28
使用Grand Central Dispatch和UINavigationController的UI问题(UI issue using Grand Central Dispatch and UINavigationController)

我正在尝试创建自己的视图,类似于UIAlertView 。 通过在我的自定义视图中添加一些子视图,然后将我的自定义视图作为子视图添加到[[[UIApplication sharedApplication].delegate window]我已经完成了这项工作。 我已经实现了自己的代表团,一旦按下警报视图中的按钮,就从窗口中删除视图。

当委托视图控制器加载时,它在后台运行一个方法,一旦完成,我在主队列上运行updateUI方法。 在此updateUI方法中显示警报视图。 我这样做是使用GCD:

dispatch_async(dispatch_get_main_queue(), ^{ [self updateUI]; });

我遇到的问题是我的委托是嵌入在导航控制器中的视图控制器,当我按“后退”以在后台方法完成之前关闭此委托视图控制器时,警报视图将显示在父级上查看控制器。 按警报视图中的按钮不会执行任何操作,因为代理现在已离开导航控制器堆栈。

有谁知道为什么这个警报视图仍然显示,以及我如何防止这种情况发生? 我认为关闭弹出委托视图控制器会阻止调用updateUI方法,但无论如何它看起来都是如此。

I am trying to create my own view that is similar to UIAlertView. I have pretty much done this by adding a few subviews to my custom view and then adding my custom view as a subview to [[[UIApplication sharedApplication].delegate window]. I have also implemented my own delegation to remove the view from the window once a button in the alert view has been pressed.

When the delegate view controller loads, it runs a method in the background, and once it is finished, I am running an updateUI method on the main queue. It is in this updateUI method that the alert view is shown. I do this using GCD:

dispatch_async(dispatch_get_main_queue(), ^{ [self updateUI]; });

The problem that I am running into is that my delegate is a view controller embedded in a navigation controller, and when I press "back" to pop this delegate view controller off before the background method finishes, the alert view will be shown on the parent view controller. Pressing the button in the alert view does not do anything, as the delegate is now off the navigation controller stack.

Does anyone know why this alert view is still shown, and how I could prevent this from happening? I would think that popping the delegate view controller off would prevent its updateUI method from being called, but it looks like it is anyway.

最满意答案

当您的视图控制器从导航堆栈中删除时,它很可能仍然由在后台运行的块保留。 因此,虽然不在屏幕上,但仍会收到您的消息。

如果我理解正确,您将在应用程序窗口中添加子视图。 这就解释了为什么即使视图控制器不在屏幕上,您的自定义警报视图仍会显示。

不一定是最好的,但避免显示警报视图的一种方法是检查视图控制器的视图的超级视图。 如果superview为nil ,则视图控制器不可见,您可以有效避免显示警报。

When your view controller is removed from the navigation stack it is most probably still being retained by your block that runs in the background. Therefore, although not on screen, it will still receive your messages.

If I have understood you correctly, you are adding a subview to your application window. That explains why your custom alert view is still being presented even when your view controller is off the screen.

Not necessarily the best, but one method to avoid the alert view from being presented, is to check for your view controller's view's superview. If the superview is nil, your view controller is not visible and you can effectively avoid displaying the alert.

更多推荐

本文发布于:2023-07-24 08:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1243481.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Dispatch   Central   Grand   issue   UI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!