2012-07-17 15 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

相關問題