在新的View Controller上显示警报

时间:2016-11-05 15:54:58

标签: ios swift swift3 uialertcontroller

我有一个按钮,可以将我发送到另一个View Controller。我正在尝试的是在下一个视图控制器上显示警报。

2 个答案:

答案 0 :(得分:1)

在新控制器的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 here的文章。他们还解释了您可以对该类进行的其他操作,例如显示操作表。

答案 1 :(得分:0)

Swift 4
使用您的函数创建extension 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>)
    }
}
相关问题