Ios 在新视图控制器上显示警报
我有一个按钮,在另一个视图控制器上发送给我。我尝试的是在下一个视图控制器上显示警报 在新控制器的Ios 在新视图控制器上显示警报,ios,swift,swift3,uialertcontroller,Ios,Swift,Swift3,Uialertcontroller,我有一个按钮,在另一个视图控制器上发送给我。我尝试的是在下一个视图控制器上显示警报 在新控制器的viewDidLoad()方法中,创建一个新的UIAlertController并如下所示显示它 let alertController = UIAlertController(title: "Default Style", message: "A standard alert.", preferredStyle: .Alert) let cancelAction = UIAlertAction(t
viewDidLoad()
方法中,创建一个新的UIAlertController
并如下所示显示它
let alertController = UIAlertController(title: "Default Style", message: "A standard alert.", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
// ...
}
alertController.addAction(cancelAction)
let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
// ...
}
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true) {
// ...
}
请注意,此示例取自NSHipster网站,该网站提供了有关iOS的精彩文章。您可以找到关于UIAlertController的文章。它们还解释了你可以用这个类做的其他事情,比如显示动作表。Swift 4
使用函数创建
UIViewController
的扩展
,以显示带有所需参数参数的警报
extension UIViewController {
func displayalert(title:String, message:String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction((UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
alert.dismiss(animated: true, completion: nil)
})))
self.present(alert, animated: true, completion: nil)
}
}
现在从视图控制器调用此函数:
class TestViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.displayalert(title: <String>, message: <String>)
}
}
类TestViewController:UIViewController{
重写func viewDidLoad(){
super.viewDidLoad()
self.displayalert(标题:,消息:)
}
}