如何在警报控制器的文本字段中设置数字键盘[swift]

Nic*_*las 13 uitextfield ios swift uialertcontroller

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

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)
Run Code Online (Sandbox Code Playgroud)

Nic*_*las 25

我自己找到了!它可能对其他人有用.

alert.addTextField(configurationHandler: { textField in
    textField.keyboardType = .numberPad
})
Run Code Online (Sandbox Code Playgroud)


Raw*_*ean 6

斯威夫特 3:

alert.addTextField { (textField) in
    textField.keyboardType = .decimalPad
}
Run Code Online (Sandbox Code Playgroud)