【问题标题】:Displaying an alert on a new View Controller在新的视图控制器上显示警报
【发布时间】:2016-11-05 15:54:57
【问题描述】:

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

【问题讨论】:

    标签: ios swift swift3 uialertcontroller


    【解决方案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 的文章。他们还解释了您可以使用该类执行的其他操作,例如显示操作表。

    【讨论】:

      【解决方案2】:

      斯威夫特 4
      使用您的函数创建 extensionUIViewController 以显示带有所需参数参数的警报

      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>)
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-10-02
        • 2018-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多