【问题标题】:How do i get a value of textField in an UIAlertController? [duplicate]如何在 UIAlertController 中获取 textField 的值? [复制]
【发布时间】:2015-05-20 08:58:49
【问题描述】:

我正在尝试从 UIAlertController 获取 textField 值,但每当我尝试输入该值并尝试将其打印出来时,输出都不会显示任何内容。

代码:

var alert = UIAlertController(title: "Enter the password", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
alert.addTextFieldWithConfigurationHandler({(textField: UITextField!) in
                textField.placeholder = "Enter text:"
                textField.secureTextEntry = true
                println(textField.text)

      })
self.presentViewController(alert, animated: true, completion: nil

【问题讨论】:

    标签: ios swift uialertcontroller


    【解决方案1】:

    试试这个代码:

    var inputTextField: UITextField?
    let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)
    let ok = UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in
        // Do whatever you want with inputTextField?.text
        println("\(inputTextField?.text)")
    })
    
    let cancel = UIAlertAction(title: "Cancel", style: .Cancel) { (action) -> Void in
    }
    alertController.addAction(ok)
    alertController.addAction(cancel)
    alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in
        inputTextField = textField
    }
    presentViewController(alertController, animated: true, completion: nil)
    

    【讨论】:

      猜你喜欢
      • 2016-04-02
      • 2019-01-18
      • 1970-01-01
      • 2015-11-02
      • 2020-06-11
      • 1970-01-01
      • 2021-07-22
      • 1970-01-01
      • 2017-12-12
      相关资源
      最近更新 更多