Swift UIAlertController Getting Text Field Text

前端 未结 2 584
我寻月下人不归
我寻月下人不归 2021-01-13 09:30

I need to get the text from the text fields in my alert view when the Input button is pressed.

func inputMsg() {

    var points = \"\"
    var outOf = \"\"
         


        
相关标签:
2条回答
  • 2021-01-13 10:03

    The UIAlertController has a textFields property. That's its text fields. Any of your handlers can examine it and thus can get the text from any of the text fields.

    0 讨论(0)
  • 2021-01-13 10:11

    As requested here is an implementation solution.

    alertController.addAction(UIAlertAction(title: "Submit", style: UIAlertActionStyle.Default,handler: {
                (alert: UIAlertAction!) in
                if let textField = alertController.textFields?.first as? UITextField{
                    println(textField.text)
                }
            }))
    

    As stated above, the alertController has a property called textFields. You can conditionally unwrap that property to safely access a text field if you have added one. In this case since there is only one text field I just did the unwrap using the first property. Hope it helps.

    0 讨论(0)
提交回复
热议问题