在新视图控制器上显示警报

时间:2022-01-31 12:57:46

I have a button that sends me on another View Controller. What I am trying is to display an alert on the next View Controller.

我有一个按钮,可以发送给我另一个视图控制器。我要在下一个视图控制器上显示一个警告。

2 个解决方案

#1


1  

In the viewDidLoad() method of the new controller, create a new UIAlertController and display it like the following

在新控制器的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) {
    // ...
}

Note that this example was taken from the NSHipster website which offers nice articles about iOS. You can find the article about UIAlertController here. They also explain other stuff you can do with that class, like display an Action Sheet for example.

请注意,这个例子是从NSHipster网站上获得的,它提供了关于iOS的好文章。你可以在这里找到关于UIAlertController的文章。它们还解释了可以使用该类做的其他事情,例如显示操作表。

#2


0  

Swift 4
Create an extension of UIViewController with your function to display alert with required parameter arguments

使用您的函数创建一个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)


      }
}


Now call this function from your view controller:

现在从视图控制器调用这个函数:

class TestViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.displayalert(title: <String>, message: <String>)
    }
}

#1


1  

In the viewDidLoad() method of the new controller, create a new UIAlertController and display it like the following

在新控制器的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) {
    // ...
}

Note that this example was taken from the NSHipster website which offers nice articles about iOS. You can find the article about UIAlertController here. They also explain other stuff you can do with that class, like display an Action Sheet for example.

请注意,这个例子是从NSHipster网站上获得的,它提供了关于iOS的好文章。你可以在这里找到关于UIAlertController的文章。它们还解释了可以使用该类做的其他事情,例如显示操作表。

#2


0  

Swift 4
Create an extension of UIViewController with your function to display alert with required parameter arguments

使用您的函数创建一个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)


      }
}


Now call this function from your view controller:

现在从视图控制器调用这个函数:

class TestViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.displayalert(title: <String>, message: <String>)
    }
}