0

UIPickerView 选择和隐藏

当我在文本字段上“触地”时,我用它来弹出一个日期选择器视图。也就是说,我需要文本字段来显示我在日期选择器视图上选择的任何内容作为其内容。

 - (IBAction)showPicker:(id)sender {

    // create a UIPicker view as a custom keyboard view
    UIDatePicker* pickerView = [[UIDatePicker alloc] init];
    [pickerView sizeToFit];
    pickerView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    //pickerView.delegate = self;
    //pickerView.dataSource = self;
    //pickerView.showsSelectionIndicator = YES;
    self.datePickerView = pickerView;  //UIDatePicker



    fromDate.inputView = pickerView;



    // create a done view + done button, attach to it a doneClicked action, and place it in a toolbar as an accessory input view...
    // Prepare done button
    UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
    keyboardDoneButtonView.barStyle = UIBarStyleBlack;
    keyboardDoneButtonView.translucent = YES;
    keyboardDoneButtonView.tintColor = nil;
    [keyboardDoneButtonView sizeToFit];

    UIBarButtonItem* doneButton = [[[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                    style:UIBarButtonItemStyleBordered target:self
                                                                   action:@selector(pickerDoneClicked:)] autorelease];

    fromDate.text = (NSString *)datePickerView.date; // fromDate is the Text Field outlet, where I am trying to display the selection on the picker.

    [keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]];

    // Plug the keyboardDoneButtonView into the text field...
    fromDate.inputAccessoryView = keyboardDoneButtonView;  

    [pickerView release];
    [keyboardDoneButtonView release];
     }

    - (IBAction)pickerDoneClicked:(id)sender {

        [fromDate resignFirstResponder];
    }

编辑:我可以为屏幕上的一个文本字段执行此操作。无论如何,第二个文本字段只会弹出一个键盘。

有什么想法可以为第二个文本字段执行此操作吗?

4

2 回答 2

0
fromDate.inputView = pickerView;

这行代码使选取器视图成为 fromDate 字段的输入视图。您需要其他日期字段的类似代码。您还需要在其他字段上设置输入附件。

创建一种特殊的方法来执行此操作并在所有文本字段上执行操作是不必要的工作。只需在其中创建选择器视图和工具栏,viewDidLoad并将它们分配给您想要它们的任何字段的输入视图和附件视图。文本字段已经有一个触摸方法,它会调出键盘。

于 2012-07-17T10:49:06.017 回答
0

只是您问题的替代解决方案。

1)UIButton在要显示的点击的文本字段上放置一个自定义UIDatePickerView

2) 将要在其上显示选取器视图的文本字段的委托设置为 nil。

fromDate.delegate = nil;

3)在该IBAction自定义按钮的(“touch down”)上写下以下代码:-

- (IBAction) customButtonAction: (id) sender
{
    [_previousTextField resignFirstResponder]; // The instance of last textfield tapped.

    [self showDatePicker];
}

4)点击选择器视图的“完成”按钮,将所选值设置为文本字段

- (IBAction)pickerDoneClicked:(id)sender 
   {
      fromDate.text = @"value from UIPickerView's selected row"
   }
于 2012-07-17T11:50:49.567 回答