【问题标题】:iPhone Application : Date Picker View as drop down listiPhone应用程序:日期选择器视图作为下拉列表
【发布时间】:2012-07-16 02:40:42
【问题描述】:

UIPickerView select and hide

当我“按下”文本字段时,我使用它来弹出一个日期选择器视图。也就是说,我需要文本字段来显示我在日期选择器视图中选择的任何内容。

在选择所需日期/时间后单击“完成”按钮后,文本字段不会显示我的输入。

我错过了什么?

- (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];
}

【问题讨论】:

  • 你实现了哪些选择器视图委托方法?
  • 我正要插入代码时,不小心按了回车,问题就贴出来了。

标签: iphone datepicker uidatepicker


【解决方案1】:

你必须为 UIControlEventValueChanged 的​​选择器设置一个选择器

 [datePicker addTarget:self
               action:@selector(setDate:)
     forControlEvents:UIControlEventValueChanged];


 - (void)setDate:(id)sender{

     NSDateFormatter *df = [[NSDateFormatter alloc] init];

     df.dateStyle = NSDateFormatterMediumStyle;

     date.text = [NSString stringWithFormat:@"%@",
                  [df stringFromDate:datePicker.date]];

}

【讨论】:

  • 下次我会记住的。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-28
  • 2015-10-13
  • 1970-01-01
  • 1970-01-01
  • 2023-02-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多