Reputation: 159
I have a set of 4 textfields. I have applied certain restriction to these textfields which can be easily understood by the code below.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString * newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if (string.length==0) {
return YES;
}
if (textField == txt_mins) {
return [[AppDelegate sharedInstance] setNumberFormat:textField newString:newString
IntegerLimit:5 FractionLimit:2 NumberType:2];
}
else if (textField == txt_units){
return [[AppDelegate sharedInstance] setNumberFormat:textField newString:newString
IntegerLimit:5 FractionLimit:2 NumberType:2];
}
// Below logic is for All 4 Modifer Textfields
// we are restricting the user to enter only max 2 characters in modifier textfields and
also automatically
// converting each entered character to the uppercase string.
if (textField==txt_modifier1 || textField==txt_modifier2 || textField==txt_modifier3 ||
textField==txt_modifier4) {
// This condition is restricting the user from entering the special characters in the
modifier textfield.
if ([string rangeOfCharacterFromSet:[[NSCharacterSet alphanumericCharacterSet]
invertedSet]].location != NSNotFound) {
// There are non-alphanumeric characters in the replacement string
return NO;
}
else{
NSString *result = [textField.text stringByReplacingCharactersInRange:range
withString:string.uppercaseString];
if (result.length <= 2)
textField.text = result;
return NO;
}
}
return YES;
}
What I actually want is that for txt_modifier1. As soon as I type my two characters in the textfield the focus should shift to the next textfield that is txt_modifier2 and similarly for the other modifier text fields. I have made these restrictions in shouldChangeCharactersInRange: method. Please suggest the way to handle this enhancement in my code.
Upvotes: 0
Views: 311
Reputation: 3011
@property UIView *firstResponder;
- (void) toggleTextfield
{
NSInteger nextTag = self.firstResponder.tag;
nextTag += 1;
UITextField *nextTextField = (UITextField *)[self.view viewWithTag:nextTag];
if (nextTextField)
{
[nextTextField becomeFirstResponder];
}
}
assign tag value to all the textfields as u want use them.
call this method when u want to shift the focus.
add this line in textFieldDidBeginEditing:
self.firstResponder = textField;
Replace this code
if (result.length <= 2)
textField.text = result
return NO;
with
if (result.length <= 2)
textField.text = result;
else [self toggleTextfield];
and add this function -
(void)textFieldDidBeginEditienter code hereng:(UITextField *)textField
{
self.firstResponder = textField;
}
Upvotes: 3
Reputation: 359
if (textField==txt_modifier1)
{
// This condition is restricting the user from entering the special characters in the
modifier textfield.
if ([string rangeOfCharacterFromSet:[[NSCharacterSet alphanumericCharacterSet]
invertedSet]].location != NSNotFound) {
// There are non-alphanumeric characters in the replacement string
return NO;
}
else{
NSString *result = [textField.text stringByReplacingCharactersInRange:range
withString:string.uppercaseString];
if (result.length <= 2)
textField.text = result;
else
[txt_modifier2 becomeFirstresponder];
return NO;
}
}
else if( textField==txt_modifier2 )
{
same code of above block just change the text filed on which you want to focus next
}
else if(textField==txt_modifier3 )
{
same code of above block just change the text filed on which you want to focus next
}
else if(textField==txt_modifier4)
{
same code of above block just change the text filed on which you want to focus next
}
Hope this helps you.Let me know the result
Upvotes: 0