user3765365 user3765365 - 2 months ago 7
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"
message:@"Message"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];


[alert show];

Bas Bas
Answer

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)