0

我正在为我的 iOS 应用程序创建一个“登录”和“创建帐户”表单。我成功实现了 UITextField 隐藏时的向上滚动。但是,既然我实现了“下一步”按钮,则不会调用“UIKeyboardDidShowNotification”,因为键盘永远不会被关闭。我需要调用keyboardWasShow 方法,以便检查活动的UITextField 是否隐藏。

   // 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];

在我实现了我的 Next 按钮(见下文)之后,不会调用 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

避免这种情况的一种方法是在设置下一个响应者之前退出响应者,这将确保keyboardWasShown调用通知。例如,根据您的代码,您可以使用以下内容:

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

虽然这可能看起来很奇怪,但应该注意键盘实际上并没有消失/重新出现。

于 2013-04-17T17:39:55.787 回答
-1

You may want to look into this

If your fields arent in a table the same sort of logic can apply to other cases.

于 2012-11-01T18:31:46.927 回答