How can I allow a UITextField to have only one text / number and navigate through multiple `UITextField` with Next / Done button
I need to handle moving through several UITextField
using the (Next / Done) button and now I have to allow only one text / number in each UITextField
, how can we do it in UITextField
as shown in the picture below
I recently used the following code and I managed to achieve it too, but I got a problem. When I first enter a text / number in UITextField
, it is entered in UITextField
and when I enter the next text / number for the second, the time is only used to go to the next UITextField
. I want to achieve that when typing text / number the second time it has to be clicked on next UITextField
and also enter the value in the nextUITextField
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.tag<10) {
if ((textField.text.length >= 1) && (string.length > 0))
{
NSInteger nextText = textField.tag + 1;
// Try to find next responder
UIResponder* nextResponder = [textField.superview viewWithTag:nextText];
if (! nextResponder)
[textField resignFirstResponder];
// nextResponder = [textField.superview viewWithTag:1];
if (nextResponder){
// Found next responder, so set it.
[nextResponder becomeFirstResponder];
return NO;
}
}
}
return YES;
}
I solve my problem using the following code, but I still have one more problem . For the last UITextField, I need its resignFirstResponder right after populating it. But at the time it fills up and user clicks next text then only resignFirstResponder happens
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.tag<10) {
if ((textField.text.length >= 1) && (string.length > 0))
{
NSInteger nextText = textField.tag + 1;
UIResponder* nextResponder = [textField.superview viewWithTag:nextText];
if (! nextResponder)
[textField resignFirstResponder];
if (nextResponder){
[nextResponder becomeFirstResponder];
UITextField* nextTextfield= (UITextField*) [textField.superview viewWithTag:nextText];
if ((nextTextfield.text.length < 1)){
[nextTextfield setText:string];
}
return NO;
}
}
}
return YES;
}
source to share
you can do this using the textfield delegate. A method you can set tags for each textbox in a sequence like 1,2,3 .... now in shouldChangeCharactersInRange
Correct method logic that makes the next block the first to respond when you enter one text / number in the text field.
as below
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ((textField.text.length >= 1) && (string.length > 0))
{
NSInteger nextText = textField.tag + 1;
// Try to find next responder
UIResponder* nextResponder = [textField.superview viewWithTag:nextText];
if (! nextResponder)
nextResponder = [textField.superview viewWithTag:1];
if (nextResponder)
// Found next responder, so set it.
[nextResponder becomeFirstResponder];
return NO;
}
return YES;
}
Edit:
If you want to display the input of text characters when jumping the textbox, you can add these lines after [nextResponder becomeFirstResponder]
add these lines below
UITextField nextTextfield= (UITextField) [textField.superview viewWithTag:nextText];
[nextTextfield setText:string];
EDIT to return the keyboard responder in the last text field.
If you want resign responder for last textfield
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.tag<10) {
if ((textField.text.length >= 1) && (string.length > 0))
{
NSInteger nextText = textField.tag + 1;
// Try to find next responder
UIResponder* nextResponder = [textField.superview viewWithTag:nextText];
if (! nextResponder)
[textField resignFirstResponder];
// nextResponder = [textField.superview viewWithTag:1];
if (nextResponder){
// Found next responder, so set it.
[nextResponder becomeFirstResponder];
UITextField *nextTextfield= (UITextField*) [textField.superview viewWithTag:nextText];
if (nextTextfield.text.length<1) {
if(nextTextfield.tag==4){
[nextTextfield setText:string];
[nextTextfield resignFirstResponder];
}else{
[nextTextfield setText:string];
}
}
return NO;
}
}
}
return YES;
}
In this we check the last text tag here. I am checking the value of tag 4 . u put ur last textfiedl tag in a state where u enters a value for the last textbox, it will reset the keyboard. hope this helps you.
source to share
To enter only numbers in a text box and set the length of the text box, try this:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
// Check for non-numeric characters
NSUInteger lengthOfString = string.length;
for (NSInteger loopIndex = 0; loopIndex < lengthOfString; loopIndex++) {
unichar character = [string characterAtIndex:loopIndex];
if (character < 48) return NO; // 48 unichar for 0
if (character > 57) return NO; // 57 unichar for 9
}
// Check for total length
NSUInteger proposedNewLength = textField.text.length - range.length + string.length;
if (proposedNewLength > 1) return NO; //set your length here
return YES;
}
source to share
setting up a textbox delegate and implementing the following delegate method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// Prevent crashing undo bug – see note below.
if(range.length + range.location > textField.text.length)
{
return NO;
}
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 25) ? NO : YES;
}