我有以下代码,它使用警报从用户那里收集电子邮件地址。我想指定键盘类型,但无法弄清楚如何在警报中执行此操作。有谁能帮助展示如何设置 .keyboardType = UIKeyboardType.emailAddress
?
var userInput: String = ""
let prompt = UIAlertController.init(title: nil, message: "Enter your email address", preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction.init(title: "OK", style: UIAlertActionStyle.default) { (action) in
userInput = prompt.textFields![0].text
if (userInput!.isEmpty) {
return
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in
print(action)
}
prompt.addTextField(configurationHandler: nil)
prompt.addAction(okAction)
prompt.addAction(cancelAction)
self.view?.window?.rootViewController?.present(prompt, animated: true, completion: nil)
最佳答案
用 addTextfield 行切换它。基本上,这是您为文本字段进行配置的地方。
prompt.addTextField { (textfield) in
textfield.keyboardType = .emailAddress
}
关于swift - 如何在警报中设置键盘类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51104642/