[SOLVED] Create a simple Alert in swift

Issue

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];

Solution

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)

Answered By – iPatel

Answer Checked By – Mary Flores (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *