我一直在努力创建一个UIAlertView在Swift,但由于某种原因,我不能得到正确的声明,因为我得到这个错误:
无法找到一个超载的'init'接受提供的
参数
我是这样写的:
let button2Alert: UIAlertView = UIAlertView(title: "Title", message: "message",
delegate: self, cancelButtonTitle: "OK", otherButtonTitles: nil)
然后我使用:
button2Alert.show()
到目前为止,它正在崩溃,我只是不能得到正确的语法。
显示UIAlertView在swift语言:-
Protocol UIAlertViewDelegate
let alert = UIAlertView(title: "alertView", message: "This is alertView", delegate:self, cancelButtonTitle:"Cancel", otherButtonTitles: "Done", "Delete")
alert.show()
显示UIAlertViewController在swift语言:-
let alert = UIAlertController(title: "Error", message: "Enter data in Text fields", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
适用于13.0以上版本
使用SceneDelegate
适用于所有类型的课程
static func showWindowAlert(alertMessage: String, inVC:UIViewController) {
DispatchQueue.main.async(execute: {
guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let sceneDelegate = windowScene.delegate as? SceneDelegate
else {
return
}
sceneDelegate.window?.rootViewController = inVC
sceneDelegate.window?.windowLevel = UIWindow.Level.alert + 1
let alert2 = UIAlertController(title: App_Name, message: alertMessage, preferredStyle: .alert)
let defaultAction2 = UIAlertAction(title: "OK", style: .default, handler: { action in
})
alert2.addAction(defaultAction2)
sceneDelegate.window?.makeKeyAndVisible()
sceneDelegate.window?.rootViewController?.present(alert2, animated: true, completion: nil)
})
}