我一直收到这个错误 'UIAlertView' was deprecated in iOS 9.0”
I keep getting this error 'UIAlertView' was deprecated in iOS 9.0"
不断收到此错误:
'UIAlertView' was deprecated in iOS 9.0: UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead
有人可以帮我编辑这段代码吗?
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)
}
如错误所述,UIAlertView
自 iOS 9.0 起已弃用。 UIAlertController
是在 iOS 8.0 中添加的。您的代码正确选择在 iOS 8.0 或更高版本下使用 UIAlertController
。
问题是您的代码尝试对 iOS 8.0 之前的任何内容使用 UIAlertView
。但是您的应用程序的部署目标未设置为 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)
不断收到此错误:
'UIAlertView' was deprecated in iOS 9.0: UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead
有人可以帮我编辑这段代码吗?
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)
}
如错误所述,UIAlertView
自 iOS 9.0 起已弃用。 UIAlertController
是在 iOS 8.0 中添加的。您的代码正确选择在 iOS 8.0 或更高版本下使用 UIAlertController
。
问题是您的代码尝试对 iOS 8.0 之前的任何内容使用 UIAlertView
。但是您的应用程序的部署目标未设置为 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)