user3765365 user3765365 - 10 months ago 54
Swift Question

How to show an alert in Swift UIAlertView not working

I want to show an alert, but UIAlertView isn't working in swift.
How can I do this?
This is how I do it in objective-c but when I try to convert it to swift code it doesn't work.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message"

[alert show];

Bas Bas
Answer Source

You should use UIAlertController

UIAlertView is deprecated.

You can show an alert with this code:

var alert = UIAlertController(title: "Alert", message: "test", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)