继续收到此错误:
' UIAlertView中'在iOS 9.0中已弃用:UIAlertView已弃用。使用UIAlertController和UIAlertControllerStyleAlert的preferredStyle代替
有人可以帮我编辑这段代码吗?
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)
}
答案 0 :(得分:0)
如错误所述,自{9.0}起,UIAlertView
已弃用。 iOS 8.0中添加了UIAlertController
。您的代码正确选择在iOS 8.0或更高版本下使用UIAlertController
。
问题是您的代码尝试将UIAlertView
用于iOS 8.0之前的任何内容。但是,您的应用程序的部署目标未针对iOS 7或更早版本设置。因此编译器告诉您UIAlertView
已被弃用。
由于您实际上不会支持iOS 8.0之前的任何内容,因此不需要if #available(iOS 8.0, *)
或UIAlertView
。只需使用UIAlertController
编写代码,不要担心UIAlertView
。
您发布的所有代码都可以简化为UIAlertController
部分:
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)