检查UIAlertController是否已经显示的最佳方法是什么? [英] What is the best way to check if a UIAlertController is already presenting?
问题描述
我有一个表格视图,该表格视图在加载时可能会返回一个NSError,我已选择将其显示在UIAlertController中.问题是,如果返回多个错误,我会在控制台中收到此错误.
I have a tableview which, when loaded, each cell could possibly return an NSError, which I have chosen to display in a UIAlertController. Problem is I get this error in the console if multiple errors are returned.
警告:尝试在已显示的MessagesMasterVC:0x14e53d800上呈现UIAlertController:0x14e64cb00
Warning: Attempt to present UIAlertController: 0x14e64cb00 on MessagesMasterVC: 0x14e53d800 which is already presenting (null)
理想情况下,理想情况下,我想在UIAlertController扩展方法中处理此问题.
Ideally, I would ideally like to handle this in my UIAlertController extension method.
class func simpleAlertWithMessage(message: String!) -> UIAlertController {
let alertController = UIAlertController(title: nil, message: message, preferredStyle: UIAlertControllerStyle.Alert)
let cancel = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
alertController.addAction(cancel)
return alertController
}
基于matt的回答,我将扩展名更改为UIViewController扩展名,它更加简洁,并节省了许多presentViewController代码.
Based on matt's answer, I changed the extension to a UIViewController extension, its much cleaner and saves lots of presentViewController code.
func showSimpleAlertWithMessage(message: String!) {
let alertController = UIAlertController(title: nil, message: message, preferredStyle: UIAlertControllerStyle.Alert)
let cancel = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
alertController.addAction(cancel)
if self.presentedViewController == nil {
self.presentViewController(alertController, animated: true, completion: nil)
}
}
推荐答案
不是UIAlertController在已经呈现",而是MessagesMasterVC.一个视图控制器一次只能显示一个其他视图控制器.因此出现错误消息.
It is not the UIAlertController that is "already presenting", it is MessagesMasterVC. A view controller can only present one other view controller at a time. Hence the error message.
换句话说,如果您已将视图控制器告知presentViewController:...
,则在关闭所提供的视图控制器之前,您将无法再次执行该操作.
In other words, if you have told a view controller to presentViewController:...
, you cannot do that again until the presented view controller has been dismissed.
您可以通过检查其presentedViewController
来询问MessagesMasterVC是否已经在提供视图控制器.如果不是nil
,请不要告诉它presentViewController:...
-它已经在提供视图控制器.
You can ask the MessagesMasterVC whether it is already presenting a view controller by examining its presentedViewController
. If not nil
, do not tell it to presentViewController:...
- it is already presenting a view controller.
这篇关于检查UIAlertController是否已经显示的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!