我正在为我的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的最佳方法是什么?我试着把它变成一个公共方法,但是当我试图手动调用它时,我一直遇到错误。
答案 0 :(得分:1)
避免这种情况的一种方法是在设置下一个响应者之前重新响应响应者,这将确保调用keyboardWasShown
通知。例如,根据您的代码,您可以使用以下内容:
...
else if (textField == self.fullName) {
[self.fullName resignFirstResponder];
[self.password becomeFirstResponder];
}
...
虽然这可能看起来很奇怪,但应该注意的是键盘实际上并没有消失/重新出现。
答案 1 :(得分:-1)
您可能需要查看this
如果您的字段在表中,则相同类型的逻辑可以应用于其他情况。