我有这段代码,用于显示警报:
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/