Home > other >  How can i use notification & alert in swift?
How can i use notification & alert in swift?

Time:02-16

    @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)
    }

hello. I'm studying swift while making a diary app.

But I'm stuck creating a delete button for a cell.

I get an error when I tap delete in alert.

How can I handle delete with notification and alert in there??

CodePudding user response:

The problem is, you are trying to display another UIAlertController on the currently presented UIAlertController.

You are presenting your UIAlertController twice.

Remove the line present(alert, animated: true, completion: nil) under let alert = ....

CodePudding user response:

The issue is because, You have presented the alert twice

Try this:

@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)

}

  • Related