Gokhan Dilek Gokhan Dilek - 17 days ago 7
Objective-C Question

Create a simple Alert in swift

It looks like alertstatus cannot be used in swift.

Can you guys please help me implementing an alternative solution to the code below?

[self alertStatus:@"Message!" :@"Alert title" :0];

Answer

Try with following code

let alert = UIAlertView()
alert.title = "Title"
alert.message = "My message"
alert.addButtonWithTitle("Ok")
alert.show()

But in iOS 8

UIAlertView is deprecated. So use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert. it should be

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