2012-06-25 193 views
19

我跟着這個線程:datepicker by clicking on textfieldiPhone顯示的日期選擇器

我輸入以下兩個協議:

@interface ViewController : UIViewController<UIActionSheetDelegate, UITextFieldDelegate> { 

然後,在實現中,我使用以下命令:

- (void)viewDidLoad { 

    textField.delegate = self; 

[super viewDidLoad] } 

最後,我把實際的代碼顯示日期選擇器(從線程)。我也把它與IB聯繫起來。

//Date Picker 
- (void)textFieldDidBeginEditing:(UITextField *)aTextField{  
    [aTextField resignFirstResponder]; 

    pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; 

    UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)]; 
    pickerView.datePickerMode = UIDatePickerModeDate; 
    pickerView.hidden = NO; 
    pickerView.date = [NSDate date]; 

    UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
    pickerToolbar.barStyle = UIBarStyleBlackOpaque; 
    [pickerToolbar sizeToFit]; 

    NSMutableArray *barItems = [[NSMutableArray alloc] init]; 

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; 
    [barItems addObject:flexSpace]; 

    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)]; 
    [barItems addObject:doneBtn]; 

    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)]; 
    [barItems addObject:cancelBtn]; 

    [pickerToolbar setItems:barItems animated:YES]; 

    [pickerViewPopup addSubview:pickerToolbar]; 
    [pickerViewPopup addSubview:pickerView]; 
    [pickerViewPopup showInView:self.view]; 
    [pickerViewPopup setBounds:CGRectMake(0,0,320, 464)];  
} 

-(void)doneButtonPressed:(id)sender{ 
    //Do something here here with the value selected using [pickerView date] to get that value 
     [pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES]; 
} 

-(void)cancelButtonPressed:(id)sender{ 
    [pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES]; 
} 

然而,當我點擊的UITextField,我得到這個:

enter image description here

我在做什麼錯?

+0

只是爲了好玩,嘗試在「textField.delegate = self;」之前放置「[super viewDidLoad]」行。在你的viewDidLoad方法中行! – trumpetlicks

+0

我剛剛嘗試過。同樣的結果:( – MrHappyAsthma

回答

57

您可以使用UITextField輸入視圖屬性。只要alloc在代碼中初始化一個UIDatePicker並將其分配給文本字段的inputView。這樣做,將顯示UIPickerView而不是鍵盤。如果您需要任何代碼隨意問:)

編輯:代碼

你的拳頭IB

@interface myViewController <UITextFieldDelegate> 
@property (nonatomic,weak) IBOutlet UITextField *myTextField; 
@end 

鉤住你的textField在實施

UIDatePicker *datePicker = [[UIDatePicker alloc]init]; 
[datePicker setDate:[NSDate date]]; 
[datePicker addTarget:self action:@selector(updateTextField:) forControlEvents:UIControlEventValueChanged];  
[self.myTextField setInputView:datePicker]; 

這將帶來當您點擊textField時,點擊UIDatePicker。請注意我添加到datePicker中的操作。

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

爲了每一個在選擇器的用戶滾動,你只需要在一些地方落實updateTextField方法在viewController

-(void)updateTextField:(id)sender 
{ 
    UIDatePicker *picker = (UIDatePicker*)self.myTextField.inputView; 
    self.myTextField.text = [NSString stringWithFormat:@"%@",picker.date]; 
} 

利用這最後的方法,每次時間更新文本字段用戶旋轉選擇器,文本框將更新!

  • 您可以使用NSDateFormatter來改善字符串的輸出。
+0

我討厭成爲一個麻煩,但你能展示一些你的意思嗎?我不遵循:P我是Iphone開發新手。 – MrHappyAsthma

+0

我已經添加了代碼,如果你需要一些進一步的解釋,只是問! – tomidelucca

+0

在執行的地方,我把UIDatePicker部分的代碼? – MrHappyAsthma

11

下面是我如何顯示日期選擇器。

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 


    self.activeTextField = textField; 

    // Create a date picker for the date field. 
    UIDatePicker *datePicker = [[UIDatePicker alloc]init]; 
    datePicker.datePickerMode = UIDatePickerModeDate; 
    datePicker.minimumDate = [NSDate dateWithTimeIntervalSinceNow:-31536000]; 
    [datePicker setDate:[NSDate date]]; 
    [datePicker addTarget:self action:@selector(updateDateField:) forControlEvents:UIControlEventValueChanged]; 

    // If the date field has focus, display a date picker instead of keyboard. 
    // Set the text to the date currently displayed by the picker. 
    if (textField.tag == 1) 
    { 
    self.date.inputView = datePicker; 
    self.date.text = [self formatDate:datePicker.date]; 
    } 
} 


// Called when the date picker changes. 
- (void)updateDateField:(id)sender 
{ 
    UIDatePicker *picker = (UIDatePicker*)self.date.inputView; 
    self.date.text = [self formatDate:picker.date]; 
} 


// Formats the date chosen with the date picker. 
- (NSString *)formatDate:(NSDate *)date 
{ 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateStyle:NSDateFormatterShortStyle]; 
    [dateFormatter setDateFormat:@"MM'/'dd'/'yyyy"]; 
    NSString *formattedDate = [dateFormatter stringFromDate:date]; 
    return formattedDate; 
} 
0

請通過這個過程 在.h文件中的的UIDatePicker

創建對象
UIDatePicker *datePicker; 

。M檔viewDidloadMethod

datePicker = [[UIDatePicker alloc]init]; 
datePicker.datePickerMode = UIDatePickerModeDate; 
datePicker.minimumDate = [NSDate date]; 
[_dateTxtFld setInputView:datePicker]; 
UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)]; 
[toolBar setTintColor:[UIColor grayColor]]; 
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(showSelectedDate)]; 
UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
[toolBar setItems:[NSArray arrayWithObjects:space,doneBtn, nil]]; 

[_dateTxtFld setInputAccessoryView:toolBar]; 

後,您可以創建ShowSele

-(void)showSelectedDate { 
    if ([_dateTxtFld isFirstResponder]) { 
     NSDateFormatter *formatter=[[NSDateFormatter alloc]init]; 
     [formatter setDateFormat:@"YYYY-MM-dd"]; 
     _dateTxtFld.text=[NSString stringWithFormat:@"%@",[formatter stringFromDate:datePicker.date]]; 
     [_dateTxtFld resignFirstResponder]; 
    } 
    else if ([_timeTxtFld isFirstResponder]) { 
    } 
} 

這是爲我工作。