【问题标题】:how to show pickerview in uitextfield not the keyboard?如何在uitextfield而不是键盘中显示pickerview?
【发布时间】:2009-05-25 09:55:31
【问题描述】:

我想在成为 UITextfield 的 FirstResponder 而不是键盘时显示 UIPickerView,并在选择器视图的文本字段中填充值。

有人知道吗?

【问题讨论】:

    标签: iphone xcode uitextfield uipickerview


    【解决方案1】:

    使用 [textfield setInputView:pickerView];

    替换系统输入视图
    inputView
    inputAccessoryView

    【讨论】:

    • 对于那些知道自己在做什么的人来说,这是正确的答案。
    • 你们知道这个的快速实现吗?
    【解决方案2】:

    编辑:此答案在撰写本文时是正确的。此后,Apple 推出了inputView。 Mafonya answer 是你现在应该做的。

    可以防止键盘弹出。将您的类设置为 UITextField 的委托:textField.delegate = self 并在头文件中的接口声明之后({ 之前)添加:<UITextFieldDelegate>

    现在实现textFieldShouldBeginEditing:

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
        // Show UIPickerView
    
        return NO;
    }
    

    如果您希望能够隐藏您的 pickerView,此方法将不起作用。然后你可以做的是创建 UITextField 的子类并覆盖 *trackingWithTouch:event: 选择器并在其中发挥你的魔力。您可能仍需要从 textFieldShouldBeginEditting: 返回 NO 以防止键盘显示。

    【讨论】:

    • 您的解决方案部分工作。我想在某些特定的 uitextfields 上显示选取器视图,而不是在所有文本字段上,并且当从填充所需文本字段的选取器视图中选择值时,选取器视图必须是隐藏。
    【解决方案3】:

    嘿,我已经粘贴了一些我之前写的代码来覆盖这个场景。有两个示例,一个带有操作表,一个没有操作表:

    - (void)textFieldDidBeginEditing:(UITextField *)myTextField{
    
                 [myTextField resignFirstResponder];
    
                 actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
    
                 [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
    
                 CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
    
                 UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
    
                  pickerView.showsSelectionIndicator = YES;
    
                  pickerView.dataSource = self;
    
                  pickerView.delegate = self;
    
                  [actionSheet addSubview:pickerView];
    
                  [pickerView release]; //NB this may result on the pickerview going black the next time you come back to the textfield, if this is the case just remove this statement
    
                  UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:NSLocalizedString(@"SUBMIT", @"")]];
    
                  closeButton.momentary = YES;
    
                   closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
    
                   closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
    
                   closeButton.tintColor = [UIColor blackColor];
    
                   [closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
    
                   [actionSheet addSubview:closeButton];
    
                   [closeButton release];
    
                   [actionSheet showInView:displayedInView];
    
                   [actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
    
    }
    

    这是没有操作表的代码:

    - (void)textFieldDidBeginEditing:(UITextField *)myTextField{
    
             [myTextField resignFirstResponder];
    
              for (int component = 0;  component < (((NSInteger)numberOfComponentsForPickerView) - 1); component++) {
    
                  NSInteger valueForRow = [[self.textField.text substringWithRange:NSMakeRange(component,1)] integerValue];
    
                   [pickerView selectRow:valueForRow inComponent:component animated:YES];
    
              }
    
               [self fadeInPickerView];
    
                [view addSubview:pickerView];
    
                [pickerView release];
    
    }
    

    可以在以下位置找到更详细的示例:http://www.buggyprogrammer.co.uk/2010/08/18/open-uipickerview-when-user-enters-textfield/

    【讨论】:

      【解决方案4】:

      将 inputview 添加到 textField。

      picker = [[UIPickerView alloc]init];
      [picker setDataSource:self];
      [picker setDelegate:self];
      [picker setShowsSelectionIndicator:YES];
      
      MyTextField.inputView = picker;
      

      【讨论】:

      • 谢谢!它对我有用,但是当我们做出选择时如何隐藏选择器?非常感谢
      • 为我工作。谢谢
      【解决方案5】:

      查看代码(有textview+tabbar和pickerview+tabbar代码)

      UITextView and UIPickerView with its own UIToolbar

      这将使pickerview辞职等。然后你需要做的就是使用pickerview委托方法来更新文本视图

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

      不要使用文本字段,而是使用带有白色背景的 UIlabel。这样,当您点击它时,键盘将不会显示。覆盖 UILabel 上的 touches 事件,当发生这种情况时,调用方法形成将显示新视图的 privious 问题。

       -(void) addToViewWithAnimation:(UIView *) theView
      

      【讨论】:

      • 我不希望键盘只显示选择器视图,我希望在我输入文本字段时显示
      • 是的,键盘几乎总是会出现。您将不得不使用另一个视图,例如 uiLabel。我已将答案编辑得更简洁。
      • 正如Blue所说,键盘总是会出现,你无法避免。另一种选择是在键盘顶部添加一个选择器视图。它们的高度相同,因此不会看到键盘。
      猜你喜欢
      • 2010-12-24
      • 2020-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      相关资源
      最近更新 更多