ios - 如何在警报 Controller 的文本字段中使用数字键盘 [swift]

标签 ios swift uitextfield uialertcontroller

我正在尝试调用具有文本字段的警报 Controller 。但我想立即显示数字键盘,因为用户应该只输入数字。 我尝试了以下但它不起作用。

let alert = UIAlertController(title: "New Rating", message: "Rate this!", preferredStyle: UIAlertControllerStyle.Alert)

  let cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: { (action: UIAlertAction!) in })

  let saveAction = UIAlertAction(title: "Save", style: .Default, handler: { (action: UIAlertAction!) in

    let textField = alert.textFields![0] as UITextField
    textField.keyboardType = UIKeyboardType.NumberPad

    self.updateRating(textField.text)
  })

  alert.addTextFieldWithConfigurationHandler { (textField: UITextField!) in }

  alert.addAction(cancelAction)
  alert.addAction(saveAction)

  self.presentViewController(alert, animated: true, completion: nil)

最佳答案

我自己找的!它可能对其他人有用。

alert.addTextField(configurationHandler: { textField in
    textField.keyboardType = .numberPad
})

关于ios - 如何在警报 Controller 的文本字段中使用数字键盘 [swift],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28594086/

相关文章:

ios - 以编程方式更改 CLLocationManager 权限警报中的文本

ios - 如何展开 - 在 Swift 中折叠 UITextView?

ios - 如何同时检查 textField 和 textView 中的相同条件?

ios - 禁止用户在编辑文本字段时与 UI 交互(键盘除外)

c# - 如何从 NSNumber 转换为 .NET 对象

iphone - viewDidLoad 在实例变量被赋值之前运行

ios - 由于缩放,使用 ImageView 进行人脸检测和缩放效果不佳?

ios - 如何检索 cellForRow(位于 :) when tableview isn't visible

ios - 如何在 iOS 中将图像转换为二进制格式?

swift - 在swift中生成一个简单的代数表达式