swift - 如何在警报中设置键盘类型

标签 swift keyboard uialertcontroller

我有以下代码,它使用警报从用户那里收集电子邮件地址。我想指定键盘类型,但无法弄清楚如何在警报中执行此操作。有谁能帮助展示如何设置 .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/

相关文章:

ios - 如果让 OR 条件

ios - UIAlertController 在 iOS 8.4.1 中不显示警报

swift - 如何在 UIAlertController 中的 UITextField 之后插入 UILabel

ios - swift -IOS : UIalertController position

Swift:要传递的 println 值

swift - 定期从 Firebase 中删除数据

ios - 自定义 UITextViewDelegate/UITextFieldDelegate 未调用

ios - 在 Swift 中接受整数输入

ios - 快速观察者不起作用

javascript - JQuery如何获取当前输入类型?