9

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

有人知道吗?

4

5 回答 5

28

使用 [textfield setInputView:pickerView];

替换系统输入视图 inputView inputAccessoryView

于 2012-04-20T15:51:55.683 回答
24

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

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

现在实施textFieldShouldBeginEditing:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    // Show UIPickerView

    return NO;
}

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

于 2009-05-25T13:45:28.417 回答
4

将输入视图添加到文本字段。

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

MyTextField.inputView = picker;
于 2012-12-16T08:20:26.830 回答
4

嘿,我已经粘贴了一些我写了一段时间的代码来涵盖这种情况。有两个示例,一个带有操作表,一个没有操作表:

- (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 &lt; (((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/

于 2010-09-30T12:51:20.280 回答
1

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

带有自己的 UIToolbar 的 UITextView 和 UIPickerView

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

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

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

 -(void) addToViewWithAnimation:(UIView *) theView
于 2009-05-25T10:05:42.993 回答