UIAlertController的UIAlertView show()行为 [英] UIAlertView show() behavior for UIAlertController

查看:485
本文介绍了UIAlertController的UIAlertView show()行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS的早期版本中,我能够在App Delegate中的UIAlertView上调用 show 。更具体地说, show 被调用:

In previous versions of iOS I was able to call show on a UIAlertView in the App Delegate. More specifically, show was called in:

func applicationDidBecomeActive(application: UIApplication)

由于 UIAlertView s忽视了视图在大多数情况下,无论用户在应用程序中的哪个位置,此解决方案都能正常工作。

Since UIAlertViews disregarded the view hierarchy in most cases, this solution worked no matter where the user was in the app.

随着 UIAlertController 这个问题变得有点棘手。 UIAlertController 现在是 UIViewController 的子类,需要像任何其他UIViewController一样呈现。虽然呈现UIAlertController从keyWindow的RootViewController的作品,它不是理想的解决方案。

With the introduction of UIAlertController this problem becomes a little trickier. UIAlertController is now a subclass of UIViewController and needs to be presented just like any other UIViewController. While presenting the UIAlertController from the keyWindow's rootViewController works, it's not the ideal solution.

有没有人对复制 [UIAlertView中查看] UIAlertController 的功能?有没有在遍历视图层次结构的情况下在应用程序上显示 UIAlertController 的方法?

Does anyone have any ideas on replicating [UIAlertView show] functionality for a UIAlertController? Any way to show the UIAlertController on app active without traversing the view hierarchy?

推荐答案

我找到了一个解决方案,我认为它比我之前发布的答案更优雅。我将复制并粘贴我发布的答案类似的问题。如果您只想查看代码,请点击我帖子底部的链接。

I figured out a solution that I believe to be more elegant than the answer I posted previously. I'll copy and paste the answer I posted to a similar question. Follow the link at the bottom of my post if you just want to see the code.

解决方案是使用额外的UIWindow。

The solution is to use an additional UIWindow.

当你想要显示你的UIAlertController时:

When you want to display your UIAlertController:


  1. 让你的窗口成为关键和可见窗口( window.makeKeyAndVisible()

  2. 只需使用普通的UIViewController实例作为新窗口的rootViewController。 ( window.rootViewController = UIViewController中()

  3. 呈现您窗口的RootViewController的你的UIAlertController

需要注意的一些事项:


  • 必须强烈引用您的UIWindow。如果它没有被强烈引用,它将永远不会出现(因为它被释放)。我建议使用一个属性,但我也成功使用了相关对象

  • 为了确保窗口显示在其他所有窗口(包括系统UIAlertControllers)之上,我设置了windowLevel。 ( window.windowLevel = UIWindowLevelAlert + 1

  • Your UIWindow must be strongly referenced. If it's not strongly referenced it will never appear (because it is released). I recommend using a property, but I've also had success with an associated object.
  • To ensure that the window appears above everything else (including system UIAlertControllers), I set the windowLevel. (window.windowLevel = UIWindowLevelAlert + 1)

最后,我有一个完成实施,如果你只是想看看。

Lastly, I have a completed implementation if you just want to look at that.

https://github.com/dbettermann/DBAlertController

这篇关于UIAlertController的UIAlertView show()行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆