简体   繁体   English

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

[英]How to have a numberpad in a textfield of an alert controller [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 3: 斯威夫特3:

alert.addTextField { (textField) in
    textField.keyboardType = .decimalPad
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 隐藏警报控制器之前检查文本字段中的值-iOS Swift - Checking the value in textfield before alert controller hides - iOS swift 是否可以从当前警报控制器(或操作表中的文本字段)内部调用警报控制器? Xcode 8,Swift 3,IOS - Is there a way to call an alert controller from inside a current alert controller (or a textfield in an action sheet)? Xcode 8, Swift 3, IOS 标签 - >警报控制器文本字段? 我可以在警报控制器的文本字段中编辑标签中的预先存在的文本吗? Swift 3,Xcode 8,IOS - Label -> Alert Controller Textfield? Can I edit pre-existing text from a label, in a textfield in an alert controller? Swift 3, Xcode 8, IOS 如何在同一视图控制器上选择文本字段(Swift 3) - how to segue a textfield on the same view controller (swift 3) ios Swift 2:扩展名-带有文本字段的警报 - ios Swift 2: extension - Alert with textfield swift4中textfield的值为0时如何显示提示? - How to display alert when the value of textfield is 0 in swift4? 如何在 swift 中为 VPMOTPView 的数字键盘上添加完成按钮 - How to add Done button on numberpad keyboard for VPMOTPView in swift Swift 5 NumberPad 扩展按钮无响应 - Swift 5 NumberPad extended buttons unresponsive 在文本字段中输入警报而不是键盘->光标停留在文本字段Swift2中 - Alert instead of keyboard for input in textfield --> cursor stays in textfield Swift2 如何使用警报 controller 按钮、swift 重新加载 viewController 内容 - How to reload viewController content using an alert controller button, swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM