Swift UIAlertController 获取文本字段文本

2023-12-05

当按下“输入”按钮时,我需要从警报视图中的文本字段中获取文本。

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)
}

As 要求的这是一个实施解决方案。

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财产。希望能帮助到你。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Swift UIAlertController 获取文本字段文本 的相关文章

随机推荐