skip to Main Content
    @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??

2

Answers


  1. 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 = ....

    Login or Signup to reply.
  2. 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)
    

    }

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search