2012-06-25 7 views
19

ho seguito questa discussione: datepicker by clicking on textfieldiPhone datario selettore sulla UITextField tocco

ho importato entrambi i seguenti protocolli:

@interface ViewController : UIViewController<UIActionSheetDelegate, UITextFieldDelegate> { 

Poi, nella realizzazione, io uso il seguente:

- (void)viewDidLoad { 

    textField.delegate = self; 

[super viewDidLoad] } 

Infine, ho inserito il codice effettivo per visualizzare il selettore di data (dal thread). Ho anche collegato tutto in 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]; 
} 

Tuttavia, quando si fa clic sul UITextField, ottengo questo:

enter image description here

Che cosa sto facendo di sbagliato?

+0

Solo per divertimento, prova a mettere la riga "[super viewDidLoad]" prima di "textField.delegate = self;" linea nel tuo metodo viewDidLoad! – trumpetlicks

+0

L'ho provato proprio ora. Stesso risultato :( – MrHappyAsthma

risposta

57

È possibile utilizzare la proprietà della vista di input UITextField. Basta assegnare init a un normale UIDatePicker nel codice e assegnarlo alla finestra di input del campo di testo. In questo modo, verrà presentato il UIPickerView anziché la tastiera. Se avete bisogno di alcun codice non esitate a chiedere :)

EDIT: codice

È pugno collegare il vostro textField con IB

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

Nell'implementazione

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

che porterà fino a UIDatePicker quando si tocca textField. Nota l'azione che ho aggiunto a datePicker.

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

Per aggiornare il textField ogni volta che l'utente scorre nel selettore, è solo bisogno di implementare il metodo updateTextField da qualche parte nel viewController

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

Con questo ultimo metodo, ogni volta che la rotazione degli utenti il selezionatore, il campo di testo si aggiornerà!

  • È possibile utilizzare uno NSDateFormatter per migliorare l'output della stringa.
+0

Odio essere una seccatura, ma potresti mostrare qualche esempio di cosa intendi? Non seguo: P Sono abbastanza nuovo per lo sviluppo di Iphone. – MrHappyAsthma

+0

ti ho aggiunto il codice, se tu – tomidelucca

+0

Nel caso dell'implementazione, inserisco la parte UIDatePicker del codice? – MrHappyAsthma

11

Di seguito è riportato il modo in cui viene visualizzato il selezionatore di date.

- (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

Si prega di passare attraverso questo processo Nel file .h creare l'oggetto per UIDatePicker

UIDatePicker *datePicker; 

in.file di 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]; 

dopo che è possibile creare 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]) { 
    } 
} 

opere di esso per me.