Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 由于-';无法显示UIAlertController'';应用程序试图以模态方式呈现活动控制器';_Ios_Swift_Uialertcontroller - Fatal编程技术网

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)

可能重复的