0

UIPickerView select and hide

I used this to pop up a date picker view when I "touch down" on a text field. That is, I need the text field to display whatever I choose on the date picker view as its contents.

Once I click the "Done" Button after selecting the required date/time, the text field doesn't show my input.

What am I missing?

- (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];
}
Community
  • 1
  • 1
esh
  • 2,842
  • 5
  • 23
  • 39

1 Answers1

1

You have to set a selector for the picker for 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]];

}
superGokuN
  • 1,399
  • 13
  • 28