【问题标题】:Get the UIAlertController from UIAlertAction?从 UIAlertAction 获取 UIAlertController?
【发布时间】:2016-01-01 14:57:29
【问题描述】:

我想调用一个函数而不是使用带有 UIAlertAction 的闭包。是否可以获得对拥有 UIAlertAction 的 UIAlertController 的引用?

alert = UIAlertController(title: "City", message: "Enter a city name", preferredStyle: UIAlertControllerStyle.Alert)
UIAlertActionStyle.Default, handler: okAlert)

//...

func okAlert(action: UIAlertAction) {
    // Get to alert here from action?
}

【问题讨论】:

    标签: swift uialertcontroller uialertaction


    【解决方案1】:

    动作没有引用其包含的警报。不过,这只是提前计划的问题。如果您需要okAlert 来引用警报控制器,那么给它那个引用:

    func okAlert(_ action: UIAlertAction, _ alert:UIAlertController) {
        // Get to alert here?
        // yes! it is `alert`!
    }
    

    你仍然需要一个闭包来捕获alert并传递它:

    let alert = UIAlertController(
        title: "City", message: "Enter a city name", preferredStyle: .Alert)
    alert.addAction(UIAlertAction(title:"OK", style:.Default, handler: {
        action in self.okAlert(action, alert)
    }))
    

    可能有一些细节需要解决。但重点是,如果你想让okAlert 住在别的地方,你可以做到。

    【讨论】:

      【解决方案2】:

      你能试试下面的代码吗:

      let alert = UIAlertController(title: "Test alert title", message: "Test alert body", preferredStyle: .Alert)
      alert.addAction(callback())
      presentViewController(alert, animated: true, completion: nil)
      

      callback:

      func callback() -> UIAlertAction {
           return UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
               print("alert action")
          })
       }
      

      我不知道你到底想要什么。但我认为您想在UIAlertController 中获得TextField。可能是下面的代码会帮助你。

      var alert: UIAlertController?
      let tfTag = 123
      override func viewDidAppear(animated: Bool) {
          super.viewDidAppear(animated)
      
          if alert == nil {
              self.alert = UIAlertController(title: "Test alert title", message: "Test alert body", preferredStyle: .Alert)
      
              let action = UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
                  // Get your TextField
                  if let tf = self.alert!.view.viewWithTag(self.tfTag) as? UITextField {
                      print("Value: \(tf.text)")
                  }
      
              })
      
              self.alert!.addAction(action)
      
              // Insert UITextField
              let textField = UITextField()
              textField.text = "Hello World"
              textField.tag = tfTag
              self.alert!.view.addSubview(textField)
              presentViewController(self.alert!, animated: true, completion: nil)
          }
      }
      

      希望有帮助!

      【讨论】:

      • 感谢您的回复。我想我的目的不明确。我需要到达 AlertController 中的 textField。我想从回调函数中得到这个。我知道我可以使用闭包。我想知道如何在不关闭的情况下处理这个问题。
      【解决方案3】:

      我最终做的是创建一个新的 UIAlertAction 子类:

      public class UIAlertActionWithAlertController : UIAlertAction {
          public weak var alertController: UIAlertController?
      }
      

      然后我将动作创建为:

      let myAction = UIAlertActionWithAlertController(title: "Action", style: .default) { (action) in
         if let alertController = (action as! UIAlertActionWithAlertController).alertController {
              // Use alertController here
         }}
      

      在将操作添加到我拥有的警报的代码中:

      alertController.addAction(myAction)
      myAction.alertController = alertController
      

      【讨论】:

        猜你喜欢
        • 2017-02-10
        • 1970-01-01
        • 2018-05-26
        • 2014-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-25
        • 1970-01-01
        相关资源
        最近更新 更多