如何在警报控制器的文本字段中使用数字键盘[swift] [英] How to have a numberpad in a textfield of an alert controller [swift]

查看:85
本文介绍了如何在警报控制器的文本字段中使用数字键盘[swift]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to call an alert controller which has a textfield. But I'd like to show immediately the numberpad since the user should input numbers only. I tried with the following but it does not work.

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)


推荐答案

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

Found it on my own! It might be useful for someone else.

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

这篇关于如何在警报控制器的文本字段中使用数字键盘[swift]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆