如何在 Swift4 中限制和警告来自 decimalPad 的文本字段输入值?
How to limit and alert textfield input values from decimalPad in Swift4?
我有一个 decimalPad 类型的文本字段。但是,如果用户键入“.”、“..”...我的数据库将出现错误。
如果 uesr 输入以上值,我想提醒 "invalid input" 语法。
目前我的 func textFieldShouldEndEditing 代码。如何添加代码?
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
if Double(textField.text!) == 0 {
let alert = UIAlertController(title: "Notice", message: "The input value can't be 0.", preferredStyle: .alert)
let ok = UIAlertAction(title: "OK", style: .default)
alert.addAction(ok)
self.present(alert, animated: false)
return false
}
return true
}
我从 here
获得参考
首先像这样将 UITextFieldDelegate
添加到您的控制器
class ViewController: UIViewController, UITextFieldDelegate
在 viewDidLoad 中添加这个
yourTextField.delegate = self
然后
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let isNumber = CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: string))
let withDecimal = (
string == NumberFormatter().decimalSeparator &&
textField.text?.contains(string) == false
)
return isNumber || withDecimal
}
步骤:1
class ViewController: UIViewController, UITextFieldDelegate
步骤:2(ViewDidLoad)
MyTextField.delegate = self
步骤:3
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if string.characters.count > 0 {
var allowedCharacters = CharacterSet.alphanumerics
let unwantedStr = string.trimmingCharacters(in: allowedCharacters)
return unwantedStr.characters.count == 0
}
return true
}
This will work for pasting strings also in to your textfield
我有一个 decimalPad 类型的文本字段。但是,如果用户键入“.”、“..”...我的数据库将出现错误。
如果 uesr 输入以上值,我想提醒 "invalid input" 语法。
目前我的 func textFieldShouldEndEditing 代码。如何添加代码?
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
if Double(textField.text!) == 0 {
let alert = UIAlertController(title: "Notice", message: "The input value can't be 0.", preferredStyle: .alert)
let ok = UIAlertAction(title: "OK", style: .default)
alert.addAction(ok)
self.present(alert, animated: false)
return false
}
return true
}
我从 here
获得参考首先像这样将 UITextFieldDelegate
添加到您的控制器
class ViewController: UIViewController, UITextFieldDelegate
在 viewDidLoad 中添加这个
yourTextField.delegate = self
然后
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let isNumber = CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: string))
let withDecimal = (
string == NumberFormatter().decimalSeparator &&
textField.text?.contains(string) == false
)
return isNumber || withDecimal
}
步骤:1
class ViewController: UIViewController, UITextFieldDelegate
步骤:2(ViewDidLoad)
MyTextField.delegate = self
步骤:3
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if string.characters.count > 0 {
var allowedCharacters = CharacterSet.alphanumerics
let unwantedStr = string.trimmingCharacters(in: allowedCharacters)
return unwantedStr.characters.count == 0
}
return true
}
This will work for pasting strings also in to your textfield