E. Tess
E. Tess

Reputation: 17

Why doesn't my alert view controller work

let alert = UIAlertController(title: "Title", message: "message", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Title", style: UIAlertActionStyle.default, handler: { action in self.alertFunc() }))

If I build this the alert view doesn't appear. What have I missed?

P.S. I know there are some similar question but to find out what they have and I have missed is hard

Upvotes: 0

Views: 75

Answers (2)

Rashwan L
Rashwan L

Reputation: 38843

You need to present it too on your current context:

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

Add that row at the end of your alert declaration:

let alert = UIAlertController(title: "Title", message: "message", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Title", style: UIAlertActionStyle.default, handler: { action in 
    self.alertFunc() 
}))
self.present(alert, animated: true, completion: nil)

Upvotes: 2

l30c0d35
l30c0d35

Reputation: 807

You have to present the alerte view on your view.

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

Upvotes: 3

Related Questions