現在のテキスト フィールドに 2 文字が入力されるとすぐに、テキスト フィールドのフォーカスが次のテキスト フィールドに移動するようにします。私はこのコードを使用していますが、return ステートメントの配置が次のような特定の問題を引き起こしていると思います。1) 最後のテキスト フィールドで 2 文字に制限されていません。2) フォーカスを次のテキスト フィールドに移動した後 前のテキスト フィールドに戻ると、2 文字以上入力できるようになります。3) textfieldDidBegin 編集メソッドに「self.firstResponder = textfield を追加したため、他の Textfields が応答しなくなりました。つまり、テキストを入力できません。3 つのメソッドすべてのコードを次に示します。:-- これはshouldCharctersInRange メソッドのコード:
(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;
}
//Added by Shikhar
// dated: 18 June, 2013
else{
NSString *result = [textField.text stringByReplacingCharactersInRange:range
withString:string.uppercaseString];
// this condition is automatically transferring the focus to the next textfield as
soon we type two characters in the\
// the current textfield.
textField.text = result;
if (result.length == 2){
[self toggleTextfield];
}
return NO;
}
}
return YES;
}
- (void) toggleTextfield
{
NSInteger nextTag = self.firstResponder.tag;
nextTag += 1;
UITextField *nextTextField = (UITextField *)[self.view viewWithTag:nextTag];
if (nextTextField)
{ [nextTextField setUserInteractionEnabled:YES];
[nextTextField becomeFirstResponder];
}
textfieldDidBegin 編集のコードは次のとおりです。
- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.firstResponder = textField;
[textField setInputAccessoryView:tb_doneToolbar];
txt_currentFocusedTextfield = textField;
CGRect textFieldRect =[self.view.window convertRect:textField.bounds
fromView:textField];
CGRect viewRect =[self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y +1.0*textFieldRect.size.height;
CGFloat numerator =midline - viewRect.origin.y- MINIMUM_SCROLL_FRACTION *
viewRect.size.height;
CGFloat denominator =(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)*
viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
animatedDistance = floor(162.0 * heightFraction);
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
誰かが助けることができれば、それは高く評価されます。