如何关闭连接到UITextField的UIPicker

时间:2012-12-18 05:28:17

标签: objective-c ios uitextfield uipickerview uipicker

我有UITextField,推送时UIPickerView出现以选择一个值。一旦选择了值,如何让UIPickerView解散。另一个帖子中有人告诉我resignFirstResponder文本字段,但我的代码无效。有任何想法吗?注意:我有两个文本字段和UI选择器,这就是为什么我有“if”'else'语句。

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == self.ageTextField)
    {
        [ageTextField resignFirstResponder];
        [agePickerView removeFromSuperview];
        return YES;
    }
    else
    {
        [relationshipTextField resignFirstResponder];
        [relationshipPickerView removeFromSuperview];
        return YES;
    }
}

3 个答案:

答案 0 :(得分:0)

尝试使用以下代码来解决您的问题。

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
 {
     if (textField == self.ageTextField) 
     {
         [ageTextField resignFirstResponder];
     } 
     else 
     {
         [relationshipTextField resignFirstResponder];
     }
     [pickerView removeFromSuperview];
     return YES; 
 }

或按照UIPickerView的以下方法重新设置键盘。

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

答案 1 :(得分:0)

请尝试在UIPickerViewDelegate方法中重新签名UITextField,如下所示。

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    [self.ageTextField resignFirstResponder];
    pickerView.hidden = YES; //Show the pickerview again when you neeed it
}

答案 2 :(得分:0)

实现UIPickerViewDelegate协议并实现以下方法:

- (void) pickerView: (UIPickerView*) pickerView
       didSelectRow: (NSInteger)     row
        inComponent: (NSInteger)     component
{
  [self.ageTextField resignFirstResponder];
}