【发布时间】:2014-08-02 01:57:12
【问题描述】:
我有一个 UIAlertController,当他们在 TouchID 屏幕上选择“输入密码”时会提示用户。在屏幕上呈现后如何恢复用户的输入?
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 内容。
【问题讨论】:
-
你要设置控制器的
delegate,然后实现委托回调 -
我认为 UIAlertController 没有委托。文档中似乎没有提到它。
-
哦,不是我的错,我把它和
UIAlertView混淆了。您应该为每个按钮将一个函数传递给handler。处理程序将在按下时调用 -
处理程序如何访问 UITextField?闭包返回 Void 并且 UITextField 的范围在闭包内。处理程序将代表单击“确定”或“取消”的操作,但我不明白这将如何让我查询输入的文本。
-
处理程序的签名是:
handler: ((UIAlertAction!) -> Void)!),因此发送者确实会被传入。您必须访问passwordPrompt.textFields才能在处理程序中获取输入的文本。
标签: swift ios8 uialertcontroller