gpt4 book ai didi

swift - 从 UIAlertController 访问输入

转载 作者:IT王子 更新时间:2023-10-29 04:59:50 25 4
gpt4 key购买 nike

我有一个 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 内容。

最佳答案

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

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)

这样你就可以避免在你的 View Controller 上有一个不必要的属性。

关于swift - 从 UIAlertController 访问输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24172593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com