2016-03-19 33 views
9

Nella mia app voglio visualizzare UIDatePicker quando l'utente fa clic sul pulsante. E quella data salva su UITextFiled.ho fatto queste cose. Il mio problema si verifica quando il selettore di date appare senza pulsante, Come può aggiungere quel pulsante. Fino ad ora ho provato.Come aggiungere un pulsante "Fatto" con DatePicker Through StoryBoard?

- (IBAction)pickerAction:(id)sender 
{ 
datePicker.datePickerMode=UIDatePickerModeDate; 
datePicker.hidden=NO; 
datePicker.date=[NSDate date]; 
[datePicker addTarget:self action:@selector(TextTitle:) forControlEvents:UIControlEventValueChanged]; 
[self.view addSubview:datePicker]; 
NSDateFormatter * df = [[NSDateFormatter alloc] init]; 
[df setDateFormat:@"M-d-yyyy"]; 
selectedDate.text=[df stringFromDate:datePicker.date]; 
} 

-(void)TextTitle:(id)sender 
{ 
NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
[df setDateFormat:@"M-d-yyyy"]; 
selectedDate.text = [NSString stringWithFormat:@"%@", 
         [df stringFromDate:datePicker.date]]; 
} 

Come posso aggiungere pulsante fatto con questo codice. per favore aiutatemi.

+0

Questo non è uno storyboard, ma la risposta più votata qui è molto semplice utilizzando una vista accessoria http://stackoverflow.com/questions/13700552/toolbar-at-the-top-of-uipickerview-in-xcode – DogCoffee

risposta

8

risposta è

datePicker=[[UIDatePicker alloc]init]; 
datePicker.datePickerMode=UIDatePickerModeDate; 
[TextField1 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:UIBarButtonItemStyleBordered target:self action:@selector(ShowSelectedDate)]; 
UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
[toolBar setItems:[NSArray arrayWithObjects:space,doneBtn, nil]]; 

[TextField1 setInputAccessoryView:toolBar]; 
+0

@magid. grazie per il montaggio. –

+1

Potrebbe essere una buona idea mostrare come chiudere il selettore di date. Che ne dici, TextField1.resignFirstResponder? Ho notato in seguito, come la gente ha creato routine elaborate per animare il raccoglitore dalla vista. Ma il resignFirstResponder lo farà per te, assumendo che TextField1 abbia il suo inputView impostato sul selettore, come in questo esempio. –

+0

Grazie! È stato utile – Raja

2

Ho aggiunto un UIToolbar con un UIBarButtonItem per il pulsante 'done' nel mio xib con il frame impostato in modo che non sia inizialmente visibile (valore y uguale all'altezza della vista padre).

Ogni volta che l'utente accede al selettore, ho modificato il fotogramma (il valore y) di UIDatePicker e UIToolbar con un'animazione in modo che scorra con il selettore dalla parte inferiore dello schermo simile alla tastiera.

Controlla il mio codice qui sotto.

- (IBAction)showPicker 
{ 
    if(pickerVisible == NO) 
    { 
     // create the picker and add it to the view 
     if(self.datePicker == nil) self.datePicker = [[[UIDatePicker alloc] initWithFrame:CGRectMake(0, 460, 320, 216)] autorelease]; 
     [self.datePicker setMaximumDate:[NSDate date]]; 
     [self.datePicker setDatePickerMode:UIDatePickerModeDate]; 
     [self.datePicker setHidden:NO]; 
     [self.view addSubview:datePicker]; 

     // the UIToolbar is referenced 'using self.datePickerToolbar' 
     [UIView beginAnimations:@"showDatepicker" context:nil]; 
     // animate for 0.3 secs. 
     [UIView setAnimationDuration:0.3]; 

     CGRect datepickerToolbarFrame = self.datePickerToolbar.frame; 
     datepickerToolbarFrame.origin.y -= (self.datePicker.frame.size.height + self.datePickerToolbar.frame.size.height); 
     self.datePickerToolbar.frame = datepickerToolbarFrame; 

     CGRect datepickerFrame = self.datePicker.frame; 
     datepickerFrame.origin.y -= (self.datePicker.frame.size.height + self.datePickerToolbar.frame.size.height); 
     self.datePicker.frame = datepickerFrame; 

     [UIView commitAnimations]; 
     pickerVisible = YES; 
    } 
} 

- (IBAction)done 
{ 
    if(pickerVisible == YES) 
    { 
     [UIView beginAnimations:@"hideDatepicker" context:nil]; 
     [UIView setAnimationDuration:0.3]; 

     CGRect datepickerToolbarFrame = self.datePickerToolbar.frame; 
     datepickerToolbarFrame.origin.y += (self.datePicker.frame.size.height + self.datePickerToolbar.frame.size.height); 
     self.datePickerToolbar.frame = datepickerToolbarFrame; 

     CGRect datepickerFrame = self.datePicker.frame; 
     datepickerFrame.origin.y += (self.datePicker.frame.size.height + self.datePickerToolbar.frame.size.height); 
     self.datePicker.frame = datepickerFrame; 
     [UIView commitAnimations]; 

     // remove the picker after the animation is finished 
     [self.datePicker performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.3]; 
    } 
} 
1

È possibile utilizzare UIView e in questo UIView aggiungere il pulsante Datepicker e Done. Pulsante Fine crea un evento Azione e in questo ti occuperai del nascondino e della visualizzazione del tuo UIView. Spero che questo aiuti