如何在警报内设置键盘类型

Sha*_*ain 1 keyboard swift 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)
Run Code Online (Sandbox Code Playgroud)

mn1*_*mn1 5

使用 addTextfield 行切换它。基本上,这是您对文本字段进行配置的地方,正如它所暗示的那样。

prompt.addTextField { (textfield) in
   textfield.keyboardType = .emailAddress
}
Run Code Online (Sandbox Code Playgroud)