Norse
Norse

Reputation: 648

How do i make this UIAlert appear

I am trying to make an alert by pressing a button "delete data". My button is in a view which is inside a navigation controller. Here is my current code

class SomeViewController: UIViewController {
    @IBAction func deleteData(sender: UIButton) {
        let alert = UIAlertController(title: "delete Data", message: "Do you want to delete the data", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: nil))
        alert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.Default, handler: nil))
    }
}

But is gives me the following error:

Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior

Upvotes: 0

Views: 63

Answers (2)

Dan Beaulieu
Dan Beaulieu

Reputation: 19954

You've created your alert view controller but you still need to present it:

self.presentViewController(alert, animated: true, completion: nil)

You can read an article on the topic on appcoda.com

Upvotes: 2

Hussein Alzand
Hussein Alzand

Reputation: 169

for showing alert use:

self.presentViewController(myAlert, animated: true, completion: nil)

and for dismiss use:

alert.dismissViewControllerAnimated(true, completion:nil)

Upvotes: 0

Related Questions