0

iOSアプリの「サインイン」および「アカウントの作成」フォームを作成しています。UITextFieldが非表示のときに上にスクロールすることを正常に実装しました。ただし、「次へ」ボタンを実装したので、キーボードが閉じられないため、「UIKeyboardDidShowNotification」は呼び出されません。アクティブなUITextFieldが非表示になっているかどうかを確認できるように、keyboardWasShowメソッドを呼び出す必要があります。

   // Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
  {
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;

CGPoint pointInSuperview = [self.view convertPoint:self.activeField.frame.origin fromView:self.scrollView];

aRect.size.height -= kbSize.height;
//added 10 to y axis because tip of origin was outside of keyboard
pointInSuperview.y +=20;

if (!CGRectContainsPoint(aRect, pointInSuperview)) {
    CGPoint scrollPoint = CGPointMake(0.0, pointInSuperview.y - (kbSize.height -15));
    NSLog(@"it is not in the rect");
    [self.scrollView setContentOffset:scrollPoint animated:YES];
   }
}

そして私にはオブザーバーがいます

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWasShown:)
                                             name:UIKeyboardDidShowNotification object:nil];

[次へ]ボタン(以下を参照)を実装した後、keyboardWasShownメソッドが呼び出されないため、アクティブなUITextFieldが非表示になっているかどうかはチェックされません。

   //functionality for next action
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
   if (textField == self.emailAddress) {
     [self.fullName becomeFirstResponder];
     [self keyboardWasShown:(NSNotification*)UIKeyboardDidShowNotification];


 }
 else if (textField == self.fullName) {
       [self.password becomeFirstResponder];
 }
else if (textField == self.password) {
    [self.confirmPassword becomeFirstResponder];
}
[textField resignFirstResponder];
return YES;
}

ユーザーが[次へ]ボタンをクリックしたときにkeyboardWasShownを呼び出すための最良のアプローチは何でしょうか。パブリックメソッドにしようとしましたが、手動で呼び出そうとするとエラーが発生し続けました。

4

2 に答える 2

1

これを回避する 1 つの方法は、次のレスポンダーを設定する前にレスポンダーを辞任することです。これにより、keyboardWasShown通知が確実に呼び出されます。たとえば、コードに基づいて、次を使用できます。

...
else if (textField == self.fullName) {
    [self.fullName resignFirstResponder];
    [self.password becomeFirstResponder];
}
...

これは奇妙に思えるかもしれませんが、キーボードが実際に消えたり現れたりするわけではないことに注意してください。

于 2013-04-17T17:39:55.787 に答える
-1

あなたはこれを調べたいかもしれません

フィールドがテーブルにない場合、同じ種類のロジックを他のケースに適用できます。

于 2012-11-01T18:31:46.927 に答える