2012-07-17 17 views
0

UIPickerView select and hideiPhone应用:日期选取器查看下拉列表

我用这个弹出日期选择器视图上的文本字段,当我“降落”。也就是说,我需要文本字段来显示我在日期选择器视图上选择的内容作为其内容。

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

我错过了什么?

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

设置选择你实现什么选择器视图的委托方法? – jrturton 2012-07-17 07:44:55

+0

我正要插入代码时,我不小心打进入和问题得到公布。 – esh 2012-07-17 07:50:50

回答

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

} 
+0

我会记住下次。谢谢。 – esh 2012-07-17 08:32:44

相关问题