如何在uitextfield中显示uipickerview而不是键盘?

时间:2009-05-25 09:55:32

标签: iphone xcode uitextfield uipickerview

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

任何人都知道这个吗?

5 个答案:

答案 0 :(得分:28)

使用[textfield setInputView:pickerView];

替换系统输入视图
inputView
inputAccessoryView

答案 1 :(得分:25)

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

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

现在实施textFieldShouldBeginEditing:

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

    return NO;
}

如果您希望能够隐藏pickerView,则此方法无效。你可以做的是创建UITextField的子类并覆盖*trackingWithTouch:event:选择器并在那些中做你的魔术。您可能仍需要从textFieldShouldBeginEditting:返回NO以防止键盘显示。

答案 2 :(得分: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/

找到

答案 3 :(得分:4)

将inputview添加到textField。

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

MyTextField.inputView = picker;

答案 4 :(得分:1)

查看代码(有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事件,当发生这种情况时,调用该方法形成将显示新视图的私密问题。

 -(void) addToViewWithAnimation:(UIView *) theView