简体   繁体   中英

I keep getting this error 'UIAlertView' was deprecated in iOS 9.0"

Keep getting this error:

'UIAlertView' was deprecated in iOS 9.0: UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead

Can someone help me edit this code?

    if #available(iOS 8.0, *) {
        let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertControllerStyle.alert)


        alert.addAction(UIAlertAction(title: alertOKTitle, style:.destructive, handler: { alertAction in
            self.okButtonPressed()
            alert.dismiss(animated: true, completion: nil)
        }))

        alert.addAction(UIAlertAction(title: alertCancelTitle, style:.cancel, handler:{ alertAction in
            self.cancelButtonPressed()
            alert.dismiss(animated: true, completion: nil)
        }))

        alert.addAction(UIAlertAction(title: alertRemindLaterTitle, style:.default, handler: { alertAction in
            self.remindLaterButtonPressed()
            alert.dismiss(animated: true, completion: nil)
        }))

        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let controller = appDelegate.window?.rootViewController

        controller?.present(alert, animated: true, completion: nil)
    } else {
        let alert = UIAlertView()
        alert.title = alertTitle
        alert.message = alertMessage
        alert.addButton(withTitle: alertCancelTitle)
        alert.addButton(withTitle: alertRemindLaterTitle)
        alert.addButton(withTitle: alertOKTitle)
        alert.delegate = self
        alert.show()
    }


}

internal func alertView(_ alertView: UIAlertView , clickedButtonAt buttonIndex: Int){

    if(buttonIndex == 0)
    {
        cancelButtonPressed()
    }
    else if(buttonIndex == 1)
    {
        remindLaterButtonPressed()
    }
    else if(buttonIndex == 2)
    {
        okButtonPressed()
    }

    alertView.dismiss(withClickedButtonIndex: buttonIndex, animated: true)

}

As the error states, UIAlertView was deprecated as of iOS 9.0. UIAlertController was added in iOS 8.0. Your code is correctly choosing to use UIAlertController under iOS 8.0 or later.

The problem is that your code tries to use UIAlertView for anything earlier than iOS 8.0. But your app's Deployment Target is not set for iOS 7 or earlier. Therefore the compiler is telling you that UIAlertView is deprecated.

Since you are not actually going to be supporting anything earlier than iOS 8.0, there is no need for the if #available(iOS 8.0, *) or for UIAlertView . Just write your code using UIAlertController and don't worry about UIAlertView .

All of the code you posted can be reduced to just the UIAlertController part:

let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertControllerStyle.alert)

alert.addAction(UIAlertAction(title: alertOKTitle, style:.destructive, handler: { alertAction in
    self.okButtonPressed()
    alert.dismiss(animated: true, completion: nil)
}))

alert.addAction(UIAlertAction(title: alertCancelTitle, style:.cancel, handler:{ alertAction in
    self.cancelButtonPressed()
    alert.dismiss(animated: true, completion: nil)
}))

alert.addAction(UIAlertAction(title: alertRemindLaterTitle, style:.default, handler: { alertAction in
    self.remindLaterButtonPressed()
    alert.dismiss(animated: true, completion: nil)
}))

let appDelegate = UIApplication.shared.delegate as! AppDelegate
let controller = appDelegate.window?.rootViewController

controller?.present(alert, animated: true, completion: nil)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM