10

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];
iPatel
  • 46,010
  • 16
  • 115
  • 137
Gokhan Dilek
  • 4,314
  • 4
  • 21
  • 24

5 Answers5

14

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)
iPatel
  • 46,010
  • 16
  • 115
  • 137
10

XCODE 10, IOS 12 And Swift 4 and above :

1. simple alert view with no action on buttons in an alert.

let alert = UIAlertController(title: "Error", message: "Something Went Wrong", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default))
self.present(alert, animated: true)

2. Alert with actions on ok and cancel button:

let refreshAlert = UIAlertController(title: "Punch Out", message: "You Will Will Punch Out", preferredStyle: UIAlertController.Style.alert)
        
refreshAlert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action: UIAlertAction!) in
            print("Handle Ok logic here")
   }))
        
refreshAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
            print("Handle Cancel Logic here")
            refreshAlert .dismiss(animated: true, completion: nil)
   }))

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

For more customization Refer to this StackOverflow Answer

Troy
  • 690
  • 1
  • 7
  • 25
NickCoder
  • 1,504
  • 2
  • 23
  • 35
3

You can do this in 3 lines as such. Make sure to add the "OK" button or "Action" which will help dismiss the alert or else you will end up with the screen frozen on the alert

    let myMessage = "This is an alert"

    let myAlert = UIAlertController(title: myMessage, message: nil, preferredStyle: UIAlertControllerStyle.Alert)

    myAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

    self.presentViewController(myAlert, animated: true, completion: nil)
3

Swift 3 iOS 10

 let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert, animated: true, completion: nil)
JP Aquino
  • 3,946
  • 1
  • 23
  • 25
1

Since edit queue is full, here is the accepted answer updated:

var alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
present(alert, animated: true)
alegelos
  • 2,308
  • 17
  • 26