由于 - '''Application tried to present modally an active controller' 未能显示 UIAlertController
Failing to show UIAlertController due to - '''Application tried to present modally an active controller'
我有一个在 UICollectionView
中显示文档的应用程序。每个 UICollectionViewCell
都有一个按钮(与 didSelectItemAt
执行的按钮不同)。这个按钮会弹出一个我使用 UIViewController
创建的自定义弹出窗口。它被呈现 Over Current Context
。此弹出窗口显示了一个选项列表,其中一个包括 Delete document
。当用户选择后一个选项时,我希望出现 UIAlertController
以确认删除。这是我面临的问题。
这是我的代码:
我得到的错误是:
Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: 'Application tried to present modally an active controller
.
自定义弹出窗口(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)
}
}
这正是你的错误所说的。您似乎在呈现视图控制器 vc
而不是 vc
中的 alert
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, animated: true, completion: nil)
到
vc.present(alert, animated: true, completion: nil)
我有一个在 UICollectionView
中显示文档的应用程序。每个 UICollectionViewCell
都有一个按钮(与 didSelectItemAt
执行的按钮不同)。这个按钮会弹出一个我使用 UIViewController
创建的自定义弹出窗口。它被呈现 Over Current Context
。此弹出窗口显示了一个选项列表,其中一个包括 Delete document
。当用户选择后一个选项时,我希望出现 UIAlertController
以确认删除。这是我面临的问题。
这是我的代码:
我得到的错误是:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller .
自定义弹出窗口(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)
}
}
这正是你的错误所说的。您似乎在呈现视图控制器 vc
而不是 vc
alert
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, animated: true, completion: nil)
到
vc.present(alert, animated: true, completion: nil)