需要 UITextField 取 5 位数字 [英] Require a UITextField to take 5 digits

查看:19
本文介绍了需要 UITextField 取 5 位数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了一个 UITextField 来显示数字键盘.我如何要求用户准确输入 5 位数字?

I have a UITextField set up to show a number pad. How do I require the user to enter exactly 5 digits?

我四处看了看,发现我应该使用 shouldChangeCharactersInRange 但我不太明白如何实现它.

I poked around and saw I should use shouldChangeCharactersInRange but I'm not quite understanding how to implement that.

推荐答案

我只会在用户离开文本字段/使用按钮验证时使用它

I'd just use this when the user leaves the textfield/validates with a button

if ([myTextField.text length] != 5){

//Show alert or some other warning, like a red text

}else{
 //Authorized text, proceed with whatever you are doing
}

现在,如果您想在用户输入时计算字符数,您可以在 viewDidLoad

Now if you want to count the chars WHILE the user is typing, you might use in viewDidLoad

[myTextfield addTarget: self action@selector(textfieldDidChange:) forControlEvents:UIControlEventsEditingChanged]

-(void)textFieldDidChange:(UITextField*)theTextField{
 //This happens every time the textfield changes
}

如果您需要更多帮助,请务必在评论中提出问题:)

Please make sure to ask questions in the comments if you need more help :)

这篇关于需要 UITextField 取 5 位数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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