5

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 = ""

    var alertController = UIAlertController(title: "Input View", message: "Please Input Your Grade", preferredStyle: UIAlertControllerStyle.Alert)

    let actionCancle = UIAlertAction(title: "Cancle", style: UIAlertActionStyle.Cancel) { ACTION in

        println("Cacle")
    }
    let actionInput = UIAlertAction(title: "Input", style: UIAlertActionStyle.Default) { ACTION in

        println("Input")
        println(points)
        println(outOf)
    }

    alertController.addAction(actionCancle)
    alertController.addAction(actionInput)
    alertController.addTextFieldWithConfigurationHandler({(txtField: UITextField!) in
        txtField.placeholder = "I got"
        txtField.keyboardType = UIKeyboardType.NumberPad
        points = txtField.text
        })



    alertController.addTextFieldWithConfigurationHandler({(txtField: UITextField!) in
        txtField.placeholder = "Out Of"
        txtField.keyboardType = UIKeyboardType.NumberPad
        outOf = txtField.text
    })
    presentViewController(alertController, animated: true, completion: nil)
}
4

2 回答 2

8

根据要求,这里是一个实施解决方案。

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

如上所述,alertController 有一个名为textFields. 如果您添加了一个文本字段,您可以有条件地解开该属性以安全地访问文本字段。在这种情况下,由于只有一个文本字段,我只是使用该first属性进行了展开。希望能帮助到你。

于 2015-04-21T15:52:59.310 回答
3

UIAlertController 有一个textFields属性。那是它的文本字段。您的任何处理程序都可以检查它,因此可以从任何文本字段中获取文本。

于 2014-10-10T18:47:06.833 回答