Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode Swift UIAlertController获取文本字段文本_Xcode_Swift_Uialertcontroller - Fatal编程技术网

Xcode Swift UIAlertController获取文本字段文本

Xcode Swift UIAlertController获取文本字段文本,xcode,swift,uialertcontroller,Xcode,Swift,Uialertcontroller,当按下输入按钮时,我需要从警报视图中的文本字段中获取文本 func inputMsg() { var points = "" var outOf = "" var alertController = UIAlertController(title: "Input View", message: "Please Input Your Grade", preferredStyle: UIAlertControllerStyle.Alert) let action

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

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

UIAlertController具有
textFields
属性。这是它的文本字段。您的任何处理程序都可以对其进行检查,从而可以从任何文本字段中获取文本。

根据请求这里是一个实现解决方案

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
属性进行了展开。希望有帮助。

你会怎么做?我已经告诉你所有要知道的事情了。警报控制器为
alertController
。其文本字段为
textFields
。他们的文本就是他们的
文本。您已经创建了一个输入按钮,并为其提供了一个操作处理程序,该操作处理程序将在按下输入按钮时运行,并且您已经将日志消息放入其中以证明这一点。没有比这更简单的了。@Unome不要用我没有写的代码污损我的帖子。我故意不提供代码。如果您想用代码添加自己的答案,请随意添加!这是完全合法的,而且比试图弄乱别人的答案要好得多。它对你有很多好处:你的答案可以被投票;OP甚至可以取消选中我的答案,然后选中你的@马特,对不起。你的回答是正确的,它帮助我解决了问题。我只是按照评论中的要求添加了一个实现示例。@Unome无需道歉;堆栈溢出需要一些时间来适应。说真的,我邀请您用自己的代码添加自己的答案。“但我不能宽恕的是你把话塞进我的嘴里。”马特补充道