首页 > 解决方案 > 如何在警报中设置键盘类型

问题描述

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

标签: swiftkeyboarduialertcontroller

解决方案


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

prompt.addTextField { (textfield) in
   textfield.keyboardType = .emailAddress
}

推荐阅读