如何制作一个将需要的文本字段 4 数字或数字 iphone

我有一个应用程序,我想在其中引入数值
UITextField

. 但我想让你只进入 4 数字。 因此,

1234

将有效但是

12345

无法介绍。 有想法,因为我可以改变它只需要一个数字价值,有限

4

数字?
已邀请:

卫东

赞同来自:

示例代码

:


- /BOOL/textField:/UITextField */textField shouldChangeCharactersInRange:/NSRange/range replacementString:/NSString */string
{
NSString *currentString = [textField.text stringByReplacingCharactersInRange:range withString:string];
int length = [currentString length];
if /length > 4/ {
return NO;
}
return YES;
}

莫问

赞同来自:

Swift 3.0 Xcode 8.3.3.

..

第一的
UITextField

安装委托,然后放置此代码...


func textField/_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String/ -> Bool {
let textstring = /textField.text! as NSString/.replacingCharacters/in: range, with: string/
let length = textstring.characters.count
if length > 4 {
return false
}
return true
}


Objective C 代码。

..


- /BOOL/textField:/UITextField */textField shouldChangeCharactersInRange:/NSRange/range replacementString:/NSString */string
{
NSString *textstring = [textField.text stringByReplacingCharactersInRange:range withString:string];
int length = [textstring length];
if /length > 4/ {
return NO;
}
return YES;
}

涵秋

赞同来自:

下面的代码帮助我建立了仅限的限制 2 图I. 4 文本字段中的数字作为输入数据。 进入两位数焦点后,会自动转到下一个文本字段。

https://i.stack.imgur.com/CCEiG.png

override func viewDidLoad// {
super.viewDidLoad//

// Set an action on EditingChanged event of textfield
txtCardDetails3.addTarget/self, action: #selector/VerifyCardViewController.textFieldDidChange/_://, forControlEvents: UIControlEvents.EditingChanged/
}

func textFieldDidChange/textField: UITextField/{

let text = textField.text

if textField.tag == 101 { // Set tag to textfield /if multiple/ during design time
if text?.utf16.count==2 {
txtCardDetails4.becomeFirstResponder// // Move to next text field
}
}
}

func textFieldDidBeginEditing/textField: UITextField/ {
textField.becomeFirstResponder//
}

func textFieldShouldReturn/textField: UITextField/ -> Bool {
textField.resignFirstResponder//
return true;
}


//User can enter two digits in textfield with tag 101 and four digits in textfield with tag 102
func textField/textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String/ -> Bool {
if let text = textField.text {

let newStr = /text as NSString/
.stringByReplacingCharactersInRange/range, withString: string/
if newStr.isEmpty {
return true
}
let intvalue = Int/newStr/

if textField.tag == 101 { print/"101"/
return /intvalue >= 0 && intvalue <= 99/ ? true : false
}
else if textField.tag == 102 { print/"102"/
return /intvalue >= 0 && intvalue <= 9999/ ? true : false
}

}
return true
}

董宝中

赞同来自:

编辑

:

首先改变你的 textFiled keyBoard 一种
UIKeyboardTypeNumberPad

, 仅限您的键盘 numeric/digit.

并给予

标签

你的他
UITextField

, 你想要申请

/只输入4位数字/

, 例如


textField.tag = 101; /// this is for, if you have multiple textFiled then it recognize that which textFiled have limit of 4 number ;


并使用以下代码方法:


- /BOOL/textField:/UITextField */textField shouldChangeCharactersInRange:/NSRange/range replacementString:/NSString */string
{
if/textField.tag != 101/
return YES;

if/string.length == 0/
return YES;

if/textField.text.length == 4/
return NO;

NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];

if /[string rangeOfCharacterFromSet:nonNumberSet].location != NSNotFound/
{
return NO;
}
return YES;
}

要回复问题请先登录注册