如何将用户的TextField输入限制为Swift中的数字?
答案 0 :(得分:36)
您可以使用UITextFieldDelegate
的{{1}}方法来限制用户对数字的输入:
shouldChangeCharactersInRange
针对Swift 3进行了更新:
func textField(textField: UITextField,
shouldChangeCharactersInRange range: NSRange,
replacementString string: String) -> Bool {
// Create an `NSCharacterSet` set which includes everything *but* the digits
let inverseSet = NSCharacterSet(charactersInString:"0123456789").invertedSet
// At every character in this "inverseSet" contained in the string,
// split the string up into components which exclude the characters
// in this inverse set
let components = string.componentsSeparatedByCharactersInSet(inverseSet)
// Rejoin these components
let filtered = components.joinWithSeparator("") // use join("", components) if you are using Swift 1.2
// If the original string is equal to the filtered string, i.e. if no
// inverse characters were present to be eliminated, the input is valid
// and the statement returns true; else it returns false
return string == filtered
}
答案 1 :(得分:1)
首先,您必须自己继承UITextViewDelegate类 课
class ViewController: UIViewController, UITextViewDelegate {
第二次添加IBOutlet
@IBOutlet weak var firstName: UITextField!
第三个必须确保该对象正在使用
override func viewDidLoad() {
super.viewDidLoad()
firstName.delegate = self
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == firstName {
let allowedCharacters = "1234567890"
let allowedCharacterSet = CharacterSet(charactersIn: allowedCharacters)
let typedCharacterSet = CharacterSet(charactersIn: string)
let alphabet = allowedCharacterSet.isSuperset(of: typedCharacterSet)
let Range = range.length + range.location > (fnameTF.text?.count)!
if Range == false && alphabet == false {
return false
}
let NewLength = (fnameTF.text?.count)! + string.count - range.length
return NewLength <= 10
}
}
答案 2 :(得分:1)
在Swift 4.1和Xcode 10中
将 UITextFieldDelegate 添加到您的班级
class YourViewController: UIViewController, UITextFieldDelegate
然后将此代码写入您的 viewDidLoad()
yourTF.delegate = self
编写此文本字段委托函数
//MARK - UITextField Delegates
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
//For numers
if textField == yourTF {
let allowedCharacters = CharacterSet(charactersIn:"0123456789")//Here change this characters based on your requirement
let characterSet = CharacterSet(charactersIn: string)
return allowedCharacters.isSuperset(of: characterSet)
}
return true
}
答案 3 :(得分:1)
对于任何寻求简短答案的人,我发现this非常有用。
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// remove non-numerics and compare with original string
return string == string.filter("0123456789".contains)
}
在XCode 10.1,Swift 4.2中工作
答案 4 :(得分:0)
iOS没有提供这样的功能,你可以指定textfield只接受数字字符。唯一的方法是, UITextFieldDelegate 方法之一,如下所示,
(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
您需要实现以下方法并截取输入的字符,并通过以下正则表达式
"^([0-9]+)?(\\.([0-9]{1,2})?)?$"
或
[NSCharacterSet decimalDigitCharacterSet]
您可以查看输入的字符是否为数字,如果匹配正则表达式或字符集,则返回 YES ,否则返回否。
答案 5 :(得分:0)