Swift 4尝试呈现ViewController,其视图不在窗口层次结构中 [英] Swift 4 Attempt to present ViewController whose view is not in the window hierarchy

查看:102
本文介绍了Swift 4尝试呈现ViewController,其视图不在窗口层次结构中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用swift 4,并且在使用Firebase注册用户时出错时,我正在尝试创建alertView.我有一个IBAction作为注册按钮,它将使用两个textfields中的文本(一个用于电子邮件,另一个用于密码)对用户进行注册.

I am using swift 4 and I am trying to create an alertView when I there is an error while signing up a user using Firebase. I have an IBAction for the sign up button which will sign the user up using text from two textfields, one for email and one for password.

基本上,我尝试在注册过程中出现错误时显示alertview,例如,空的textfield.

I am basically trying to show an alertview when there is an error with the sign up process, for example there is an empty textfield.

我已将该功能的屏幕快照附加到发生的位置.我知道我实际上收到了一个错误,因为如果有一个print语句,则会输出一个错误.

I have attached a screenshot of the function to where that is occuring. I know that I am in fact getting an error because the print statement outputs an error if there is one.

无论是否存在错误,都不会显示警报视图,并且该应用程序将执行segue.

Regardless of if there is an error or not, there is no alert view showing up and the app performs the segue regardless.

2019-01-15 21:40:26.368924-0500 Pronto [9036:225268]警告:尝试 呈现 他的观点不在 窗口层次结构

2019-01-15 21:40:26.368924-0500 Pronto[9036:225268] Warning: Attempt to present on whose view is not in the window hierarchy

这是我现在显示的用于alertview的输出. 我看过所有其他有关此问题的文章,但似乎都没有.

This is the output that I am getting for the alertview now showing up. I have looked at all the other posts about this same issue but none seem to work.

推荐答案

由于您的视图层次结构而发生此问题.

您需要找出什么是当前/最高级视图控制器 查看层次结构并显示您的警报.

You need to find out what is your Current/Topmost view controller in view hierarchy and present your alert over it.

要查找最顶部的视图控制器,请使用以下代码:

func getTopMostViewController() -> UIViewController? {
    var topMostViewController = UIApplication.shared.keyWindow?.rootViewController

    while let presentedViewController = topMostViewController?.presentedViewController {
        topMostViewController = presentedViewController
    }

    return topMostViewController
}

并在最上方的视图控制器上显示警报,并使用主线程来显示警报,因为闭包可能在另一个线程上起作用.

DispatchQueue.main.async { 
    getTopMostViewController()?.present(alertController, animated: true, completion: nil)
}

请参考以下堆栈答案: Swift 3尝试演示其视图不在窗口层次结构中

Please refer to this stack answer: Swift 3 Attempt to present whose view is not in the window hierarchy

这篇关于Swift 4尝试呈现ViewController,其视图不在窗口层次结构中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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