我在 View 中有四个文本字段。 前三个是用标准键盘编辑的。 最后一个是使用选择器编辑的,该选择器作为文本字段的 inputView 属性。 这很酷,因为它可以处理上下滑动。
假设我正在使用标准键盘在前三个文本字段之一中输入内容。 现在我点击第四行。 我希望标准键盘缩回并且选择器向上滑动。 现在它只是立即交换。
我尝试使用 textField 委托(delegate)方法编写逻辑以在标准键盘上 resignFirstResponder 并使用选择器成为第一响应者,但它仍然会立即发生变化。这是代码。
- (void)textFieldDidBeginEditing:(UITextField *)textField{
//If we go from a keyboard field to a picker field
if ((self.textFieldBeingEdited == self.nameField || self.locationField || self.controllerSNField)){
if (textField == equipTypeField) {
//Put away the keyboard
[self.textFieldBeingEdited resignFirstResponder];
//Show the Picker
[textField becomeFirstResponder];
}
}
}
如果我从第四个 View 转到任何其他 View ,我还需要编写逻辑来向下滑动选择器并向上滑动键盘。但如果我能得到前半部分,那么后半部分应该是显而易见的。
知道如何在不进入动画 block 的情况下做到这一点吗?也许有通知?
谢谢。
最佳答案
问题可能是在调用该方法时您已经开始编辑,因此对 resignFirstResponder
的调用不会执行任何操作,因为它不再是第一响应者。也许如果你让它再次成为第一响应者,它可能会起作用。但随后在 equipTypeField
上调用 becomeFirstResponder
可能会导致 textFieldDidBeginEditing
再次被调用,从而使您陷入无限循环。毫无疑问,这是一个困境。但至少,我会尝试这个:
//So that the keyboard that animates out is the one that was previously showing
[self.textFieldBeingEdited becomeFirstResponder];
//Animate the keyboard out
[self.textFieldBeingEdited resignFirstResponder];
//Don't let the user screw anything up
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
//Animate the new keyboard in a little while later
[textField performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.25];
//Let the user interact with the application again after the animation completes
[[UIApplication sharedApplication] performSelector:@selector(endIgnoringInteractionEvents) withObject:nil afterDelay:0.25];
这仍然留下了将该代码放在哪里的问题,因为如果将其放在 textFieldDidBeginEditing
中,则可能会出现无限循环。我的建议是尝试一下,看看会发生什么。如果它有效,那就太好了,否则,请放入某种 bool 标志或其他东西,以确保在调用 didFinishEditing 之前只调用它一次。
关于iphone - 如何向下滑动一个键盘 向上滑动自定义键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4834392/