我如何在 swift 中使用通知和警报?

How can i use notification & alert in swift?

    @IBAction func tapDeleteButton(_ sender: UIButton) {
        let alert = UIAlertController(title:"Are you sure?",message: "Do you really want to delete?",preferredStyle: UIAlertController.Style.alert)
                    let cancle = UIAlertAction(title: "Cancel", style: .default, handler: nil)
                    alert.addAction(cancle)
                    present(alert, animated: true, completion: nil)
        let delete = UIAlertAction(title: "Delete", style: .destructive){(_) in
            let uuidString = self.diary?.uuidString
            NotificationCenter.default.post(name: NSNotification.Name("deleteDiary"), object: uuidString, userInfo: nil)
        }
                    alert.addAction(delete)
                    present(alert, animated: true, completion: nil)
    }

你好。 我正在学习swift,同时正在制作日记应用程序。

但我无法为单元格创建删除按钮。

当我在警报中点击删除时出现错误。

我如何处理带有通知和警报的删除?

问题是,您正试图在当前显示的 UIAlertController 上显示另一个 UIAlertController

UIAlertController 展示了两次。

删除 let alert = ... 下的行 present(alert, animated: true, completion: nil)

问题是因为,您已两次显示警报

试试这个:

@IBAction func tapDeleteButton(_ sender: UIButton) {
let alert = UIAlertController(title:"Are you sure?",message: "Do you really want to delete?",preferredStyle: UIAlertController.Style.alert)

let cancle = UIAlertAction(title: "Cancel", style: .default, handler: nil)
let delete = UIAlertAction(title: "Delete", style: .destructive){(_) in
    let uuidString = self.diary?.uuidString
    NotificationCenter.default.post(name: NSNotification.Name("deleteDiary"), object: uuidString, userInfo: nil)
}

alert.addAction(cancle)
alert.addAction(delete)
present(alert, animated: true, completion: nil)

}