0

I have an issue with a simple alert view that used to work but since it has changed in iOS 8, Xcode tells me I have to have a "if available version check" but the more I do what it tells me the more errors I get. What is the correct way of having a UIAlertView that works with different iOS versions with a simple title, message and Ok button? This is the code I have been using which used to work fine:

let Alert = UIAlertController(title: "This is the title", message: "This is the message", preferredStyle: .Alert)

let DismissButton = UIAlertAction(title: "OK", style: .Cancel, handler: {

    (alert: UIAlertAction) -> Void in

})

Alert.addAction(DismissButton)

self.presentViewController(Alert, animated: true, completion: nil)

EDIT: With the research/suggestions of Xcode I have come up with the following code. The error messages I am now getting are " use of unresolved identifier 'alert' " on the last two lines.

if activePlace == -1 {

    if #available(iOS 8.0, *) {
        let Alert = UIAlertController(title: "No place Selected", message: "Please press on back button and select a place in the list to use directions", preferredStyle: .Alert)
    } else {
        // Fallback on earlier versions
    }

    if #available(iOS 8.0, *) {
        let DismissButton = UIAlertAction(title: "OK", style: .Cancel, handler: {

            (alert: UIAlertAction) -> Void in

        })
    } else {
        // Fallback on earlier versions
    }

    Alert.addAction(DismissButton)

    self.presentViewController(Alert, animated: true, completion: nil)
stephen D
  • 255
  • 1
  • 5
  • 16
  • 4
    You can't use `UIAlertController` in iOS 7. Just use `UIAlertView` for all versions of iOS until you drop support for iOS 7. – rmaddy Feb 04 '16 at 17:42
  • Thank you for your answer @rmaddy Do you mean all I have to change is transform UIAlertController into UIAlertView ? unfortunately I have tried this and it doesn't work, I still get the same suggestions from xcode – stephen D Feb 04 '16 at 17:52
  • Your question is lacking a lot of relevant details about the messages, suggestions, and errors you are seeing. – rmaddy Feb 04 '16 at 17:53
  • I have just updated the question @rmaddy , thanks – stephen D Feb 04 '16 at 18:04
  • You should only have one `if/else` statement. Put all of the code for the `UIAlertController` in the `if` part and all of the code for the `UIAlertView` in the `else` part. This is just like the answers in the duplicate question. – rmaddy Feb 04 '16 at 18:04
  • This means I can not have an else statement if my if condition to have the alert view isn't true ? @rmaddy – stephen D Feb 04 '16 at 18:22
  • No, it doesn't mean that. I meant you should only have on `if #available.../else` statement to show either the `UIAlertController` or the `UIAlertView`. Look at the answers for the duplication question. – rmaddy Feb 04 '16 at 18:24

0 Answers0