ios - PresentViewController 不工作

标签 ios swift uialertcontroller

我有这段代码,用于显示警报:

func arraysize() -> [Int]? {

    let title = "Your file is empty"
    let message = "Please refer to the instructions"
    let okText = "OK"

    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
    let okayButton = UIAlertAction(title: okText, style: UIAlertActionStyle.Cancel, handler: nil)
    alert.addAction(okayButton)

    do{
        ....

        if array?.isEmpty == true{
            print("the array is empty")
            presentViewController(alert, animated: true, completion: nil)
        }            

    } 
    catch let error {
        print(error)
    }

    return array
}

但是我的警报没有显示。它曾经显示过,尽管我没有更改任何代码,但它不再对我有用。有人可以帮助我理解我做错了什么吗?

附注我也收到此警告:

[16632:713433] Warning: Attempt to present UIAlertController whose view is not in the window hierarchy!

可能是这个吗?

最佳答案

正如 @rmaddy 在评论中解释的那样,我的问题是我试图显示来自 Controller 的警报,但该警报从未出现在实际屏幕上。我通过从屏幕上显示的 Controller 调用此方法来修复此问题,并且它有效。

关于ios - PresentViewController 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36666440/

相关文章:

ios - ios8 中 uialertview 中的复选框

ios - Swift 委托(delegate)返回 nil

ios - UIAlertController 子类问题 swift

iphone - 如何将 "2011-01-12T14:17:55.043Z"之类的日期字符串转换为 1294841716 之类的长字符串?

ios - 如何根据 Apple 标准检查我的应用是否兼容 iOS7?

swift - 如何定义在运行使用自定义初始值设定项的结构实例时应运行的自定义闭包 (SwiftUI)

swift - 在 mac OS 上使用 swift 4 拖放

ios - 将图像转换为 CVPixelBuffer 用于机器学习 Swift

ios - Photoshop 的选择性颜色和颜色平衡滤镜如何工作?

swift - 从 FIRUser 的 uid 中获取