我有一个UIAlertController,当用户在TouchID屏幕上 Select "输入密码"时,它会提示用户.在屏幕上显示后,如何恢复用户的输入?

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知识.

推荐答案

我知道已经发布了一些 comments 来回答这个问题,但是一个答案应该让解决方案更加明确.

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

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

Swift相关问答推荐

什么是Swift Concurrency中任务组的正常退出

启用完成并发判断以及如何解决警告(续)

如何在Swift中获得与Python相同的HMAC—SHA512签名?

.onReceive NSWindow.CloseNotify是否会为App中的每个窗口调用?

如何在visionOS上删除PhotosPicker背景 colored颜色 ?

NSWindow中以编程方式创建的TextField快捷方式与SwiftUI

显示第二个操作紧接在另一个操作后的工作表在SwiftUI中不起作用

如何在自定义视图中读取修改符子元素?

Observable.create 捕获行为

在Swift中如何将NSUrl转换为本地路径URL

列表不显示信息

PassthroughSubject 的 AsyncPublisher 值属性未生成所有值

如何在 swift 5.0 中获取当前行

为什么 id 不能通过 struct 从 Objective-C 移植到 Swift?

动画偏移正在 destruct 按钮 SwiftUI

Vapor 4 身份验证问题 [用户未通过身份验证]

iOS SwiftUI - 无法转换 ObservedObject 类型的值

来自数据的 SwiftUI 图像

是 swift 中另一个日期的同一周、月、年的日期

如何使用 Swift 将文本文件逐行加载到数组中?