Swift UIAlertController Getting Text Field Text

前端 未结 2 612
我寻月下人不归
我寻月下人不归 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: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.

提交回复
热议问题