【问题标题】:UIPickerView : Keyboard not hidingUIPickerView:键盘不隐藏
【发布时间】:2012-05-12 11:15:19
【问题描述】:

我有3个UITexfield,用户可以填写前两个,但最后一个是类别字段,点击时显示一个UIPickerView。

到目前为止,我所做的是:

  • 用户单击分类文本字段 -> 出现选择器视图
  • 然后用户点击另一个文本字段 -> 选择器视图消失,键盘出现

但知道我想在用户再次单击类别文本字段并显示选择器视图时隐藏键盘。

这是我目前所拥有的:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if(textField == categoryTextField){
        [categoryTextField resignFirstResponder];
        [UIView beginAnimations:@"picker" context:nil];
        [UIView setAnimationDuration:0.3];
        pickerView.transform = CGAffineTransformMakeTranslation(0,-236);
        [UIView commitAnimations];
    }else{
        [UIView beginAnimations:@"picker" context:nil];
        [UIView setAnimationDuration:0.3];
        pickerView.transform = CGAffineTransformMakeTranslation(0,236);
        [UIView commitAnimations];
    }
}

但它不起作用。有没有想过为什么?

【问题讨论】:

  • 您可能想查看 resignFirstResponder/becomeFirstResponder 方法。我在键盘和pickerView 上遇到过类似的问题。

标签: objective-c ios uitextfield uipickerview


【解决方案1】:

你太难了。将选取器视图设置为文本字段的输入视图,操作系统将为您处理动画。随着每个字段成为第一响应者,将显示适当的输入视图(文本字段的默认键盘,第三个字段的选择器)。你不需要自己做任何动画。

【讨论】:

  • 好吧,这行得通,但我失去了动画,知道如何保留它们吗?
【解决方案2】:

我真的不明白你的问题来自哪里。我认为您的翻译可能会使pickerView 移动得太远。你应该试试看:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if(textField == categoryTextField){
        [categoryTextField resignFirstResponder];
        [UIView beginAnimations:@"picker" context:nil];
        [UIView setAnimationDuration:0.3];
        CGRect frame = pickerView.frame;
        frame.origin.y = 480 - 236; // assuming your on an iPhone and your picker appears from bottom
        pickerView.frame = frame;
        [UIView commitAnimations];
    }else{
        [UIView beginAnimations:@"picker" context:nil];
        [UIView setAnimationDuration:0.3];
        CGRect frame = pickerView.frame;
        frame.origin.y = 480; // assuming your on an iPhone and your picker disappears to bottom
        pickerView.frame = frame;
        [UIView commitAnimations];
    }
}

希望有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-17
    • 2010-10-01
    • 2020-10-12
    • 2012-04-19
    • 1970-01-01
    • 2022-12-14
    • 1970-01-01
    相关资源
    最近更新 更多