Ios 由于-';无法显示UIAlertController'';应用程序试图以模态方式呈现活动控制器';
我有一个在Ios 由于-';无法显示UIAlertController'';应用程序试图以模态方式呈现活动控制器';,ios,swift,uialertcontroller,Ios,Swift,Uialertcontroller,我有一个在UICollectionView中显示文档的应用程序。每个UICollectionViewCell都有一个按钮(与didSelectItemAt执行的按钮不同)。此按钮将显示一个自定义弹出窗口,我使用UIViewController创建了该弹出窗口。它在当前上下文中显示。此弹出窗口显示选项列表,其中包括删除文档。当用户选择后一个选项时,我希望出现一个UIAlertController来确认删除。这是我面临的问题 这是我的密码: 我得到的错误是: 由于未捕获异常“NSInvalidArg
UICollectionView
中显示文档的应用程序。每个UICollectionViewCell
都有一个按钮(与didSelectItemAt
执行的按钮不同)。此按钮将显示一个自定义弹出窗口,我使用UIViewController
创建了该弹出窗口。它在当前上下文中显示。此弹出窗口显示选项列表,其中包括删除文档
。当用户选择后一个选项时,我希望出现一个UIAlertController
来确认删除。这是我面临的问题
这是我的密码:
我得到的错误是:
由于未捕获异常“NSInvalidArgumentException”而终止应用程序,
原因:“应用程序试图以模态方式呈现活动控制器
自定义弹出窗口(UIViewController)
UICollectionView:
class CollectionViewFolder: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate ,UICollectionViewDelegateFlowLayout, MoreInfoDocument, MoveFolder, ScanObjectMovedFolder, DismissOptionShowDeleteAlert{
// SHOW DELETE CONFIRMATION ALERT
func showDeleteAlert() {
Alerts.deleteDocumentConfirm(on: self) {
// DELETE DOCUMENT FROM SERVER
print("Delete document ...")
}
}
}
import Foundation
import UIKit
struct Alerts {
private static func showBasicAlert(on vc: UIViewController, with title: String, message: String, action: @escaping (() -> ())){
let alert = UIAlertController.init(title: title, message: message, preferredStyle: .alert)
let okAction = UIAlertAction.init(title: "OK", style: .default) { (UIActionAlert) in
action()
}
let cancelAction = UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil)
alert.addAction(okAction)
alert.addAction(cancelAction)
DispatchQueue.main.async {
vc.present(vc, animated: true, completion: nil)
}
}
static func deleteDocumentConfirm(on vc: UIViewController, action: @escaping (() -> ())){
showBasicAlert(on: vc, with: "Please Confirm Delete", message: "", action: action)
}
}
UIAlertController结构:
class CollectionViewFolder: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate ,UICollectionViewDelegateFlowLayout, MoreInfoDocument, MoveFolder, ScanObjectMovedFolder, DismissOptionShowDeleteAlert{
// SHOW DELETE CONFIRMATION ALERT
func showDeleteAlert() {
Alerts.deleteDocumentConfirm(on: self) {
// DELETE DOCUMENT FROM SERVER
print("Delete document ...")
}
}
}
import Foundation
import UIKit
struct Alerts {
private static func showBasicAlert(on vc: UIViewController, with title: String, message: String, action: @escaping (() -> ())){
let alert = UIAlertController.init(title: title, message: message, preferredStyle: .alert)
let okAction = UIAlertAction.init(title: "OK", style: .default) { (UIActionAlert) in
action()
}
let cancelAction = UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil)
alert.addAction(okAction)
alert.addAction(cancelAction)
DispatchQueue.main.async {
vc.present(vc, animated: true, completion: nil)
}
}
static func deleteDocumentConfirm(on vc: UIViewController, action: @escaping (() -> ())){
showBasicAlert(on: vc, with: "Please Confirm Delete", message: "", action: action)
}
}
这正是你的错误所说的。您似乎正在显示视图控制器vc
,而不是vc
private static func showBasicAlert(on vc: UIViewController, with title: String, message: String, action: @escaping (() -> ())){
let alert = UIAlertController.init(title: title, message: message, preferredStyle: .alert)
let okAction = UIAlertAction.init(title: "OK", style: .default) { (UIActionAlert) in
action()
}
let cancelAction = UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil)
alert.addAction(okAction)
alert.addAction(cancelAction)
DispatchQueue.main.async {
vc.present(alert, animated: true, completion: nil) // You should be presenting the alert here.
}
}
你错了。您需要显示警报,而不是vc
用您的代码替换下面的内容
struct Alerts {
private static func showBasicAlert(on vc: UIViewController, with title: String, message: String, action: @escaping (() -> ())){
let alert = UIAlertController.init(title: title, message: message, preferredStyle: .alert)
let okAction = UIAlertAction.init(title: "OK", style: .default) { (UIActionAlert) in
action()
}
let cancelAction = UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil)
alert.addAction(okAction)
alert.addAction(cancelAction)
DispatchQueue.main.async {
vc.present(alert, animated: true, completion: nil)
}
}
换行
vc.present(vc,动画:true,完成:nil)
到
vc.present(警报,动画:true,完成:nil)
可能重复的