我有一个应用程序,可以显示UICollectionView
中的文档。每个UICollectionViewCell
都有一个按钮(与didSelectItemAt
所执行的按钮不同)。此按钮将弹出一个自定义弹出窗口,该弹出窗口是我使用UIViewController
创建的。显示为Over Current Context
。此弹出窗口显示了一个选项列表,其中包括Delete document
。当用户选择后一个选项时,我希望出现UIAlertController
以确认删除。这是我面临的问题。
这是我的代码:
我得到的错误是:
由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序, 原因:“应用程序尝试以模态形式显示活动控制器
自定义弹出窗口(UIViewController)
protocol DismissOptionShowDeleteAlert {
func showDeleteAlert()
}
class MoreOptionsOnPDFViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
var showDeleteAlertDelegate: DismissOptionShowDeleteAlert!
/ TAP ON ROW
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
...
}else if indexPath == [0,4]{ // DELETE DOCUMENT
DispatchQueue.main.async {
self.dismiss(animated: true) {
self.showDeleteAlertDelegate.showDeleteAlert()
}
}
}
...
}
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 ...")
}
}
}
UIAlertController结构:
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)
}
}
答案 0 :(得分:2)
这正是您的错误所说的。您似乎正在展示视图控制器vc
,而不是alert
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.
}
}
答案 1 :(得分:1)
您提出的错误。您需要显示的警报不是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)
}
}
答案 2 :(得分:1)
替换行
vc.present(vc, animated: true, completion: nil)
到
vc.present(alert, animated: true, completion: nil)