从UIAlertController访问输入 [英] Access input from UIAlertController

查看:146
本文介绍了从UIAlertController访问输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIAlertController,当他们在TouchID屏幕上选择输入密码时会提示用户。如何在屏幕上显示后恢复用户的输入?

I've got a UIAlertController which is prompted to the user when they choose "Enter Password" on the TouchID screen. How do I recover the user's input after presenting this on screen?

let passwordPrompt = UIAlertController(title: "Enter Password", message: "You have selected to enter your passwod.", preferredStyle: UIAlertControllerStyle.Alert)
passwordPrompt.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil))
passwordPrompt.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

passwordPrompt.addTextFieldWithConfigurationHandler({(textField: UITextField!) in
                        textField.placeholder = "Password"
                        textField.secureTextEntry = true
                        })

presentViewController(passwordPrompt, animated: true, completion: nil)

我知道OK按钮应该有一个处理程序,但是现在这段代码并没有真正做任何事情,但我想显示文本的输出FIEL d通过println。这真的只是一个测试应用程序,让我学习Swift和一些新的API。

I know the OK button should probably have a handler, but right now this code doesn't really do anything, but I want to display the output of the text field through a println. This is really just a test app for me to learn Swift and some new API stuff.

推荐答案

我知道评论已发布到回答这个问题,但答案应该明确解决方案。

I know comments have been posted to answer the question, but an answer should make the solution explicit.

@IBOutlet var newWordField: UITextField
func wordEntered(alert: UIAlertAction!){
    // store the new word
    self.textView2.text = deletedString + " " + self.newWordField.text
}
func addTextField(textField: UITextField!){
    // add the text field and make the result global
    textField.placeholder = "Definition"
    self.newWordField = textField
}

// display an alert
let newWordPrompt = UIAlertController(title: "Enter definition", message: "Trainging the machine!", preferredStyle: UIAlertControllerStyle.Alert)
newWordPrompt.addTextFieldWithConfigurationHandler(addTextField)
newWordPrompt.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil))
newWordPrompt.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: wordEntered))
presentViewController(newWordPrompt, animated: true, completion: nil)

这会显示一个带有文本字段和两个操作按钮的提示。完成后它会读取文本字段。

This displays a prompt with a text field and two action buttons. It reads the text field when done.

这篇关于从UIAlertController访问输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆