首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >访问来自UIAlertController的输入

访问来自UIAlertController的输入
EN

Stack Overflow用户
提问于 2014-06-12 05:07:03
回答 4查看 34.9K关注 0票数 49

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

代码语言:javascript
复制
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按钮可能应该有一个处理程序,但是现在这段代码并没有真正做任何事情,但是我想通过println显示文本字段的输出。这真的只是一个测试应用,让我学习Swift和一些新的API。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-06-26 06:03:20

我知道已经发布了一些评论来回答这个问题,但答案应该让解决方案变得明确。

代码语言:javascript
复制
@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)

这将显示一个提示,其中包含一个文本字段和两个操作按钮。完成后,它会读取文本字段。

票数 48
EN

Stack Overflow用户

发布于 2014-09-08 02:57:54

我已经写了一个blog post来探索新的API。您只需在闭包中捕获一个局部变量,就可以运行了。

代码语言:javascript
复制
var inputTextField: UITextField?
let passwordPrompt = UIAlertController(title: "Enter Password", message: "You have selected to enter your password.", preferredStyle: UIAlertControllerStyle.Alert)
passwordPrompt.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil))
passwordPrompt.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
    // Now do whatever you want with inputTextField (remember to unwrap the optional)
}))
passwordPrompt.addTextFieldWithConfigurationHandler({(textField: UITextField!) in
    textField.placeholder = "Password"
    textField.secureTextEntry = true
    inputTextField = textField
 })

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

这样就避免了在视图控制器上有一个不必要的属性。

票数 66
EN

Stack Overflow用户

发布于 2014-08-12 06:28:01

在你的闭包中这样做:

代码语言:javascript
复制
let tf = alert.textFields[0] as UITextField

Here is a gist

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24172593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档