私は4つのテキストフィールドのセットを持っています。以下のコードで簡単に理解できるように、これらのテキストフィールドに特定の制限を適用しました。
- (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;
}
私が実際に欲しいのは、txt_modifier1. テキスト フィールドに 2 文字を入力するとすぐに、次のテキスト フィールドである txt_modifier2 にフォーカスが移ります。他の修飾子テキスト フィールドも同様です。これらの制限は shouldChangeCharactersInRange: メソッドで行いました。私のコードでこの機能強化を処理する方法を提案してください。