swift - 如何在警报中设置键盘类型
问题描述
我有以下代码,它使用警报从用户那里收集电子邮件地址。我想指定键盘类型,但无法弄清楚如何在警报中执行此操作。有没有人可以帮助展示如何设置.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
}
推荐阅读
- python - 更新 download.default_directory chromedriver 以下载不同的文件
- f# - 如何在 F# 代码中调试没有详细信息的 TypeLoadException?
- android - 从 firebase 检索 hashmap 以填充 FirebaseRecycler,null HashMap
- azure - OpenSIPS Azure 信令不转发呼叫
- batch-file - 变量是净使用批次
- spring-mvc - 如何将 Thymeleaf th:field 与 java 8 LocalDate 绑定
- postgresql - 如何使用 PostgreSQL 和 TypeORM 进行查询)其中表具有 JSON 类型列
- c++ - `IDispatch::GetTypeInfo()` 接口是否正常?
- amazon-web-services - 每条路由有 1 个 Lambda 函数会更好吗?还是 1 个处理子路由的 Lambda?
- c# - 在分配给模型之前验证 {} 值