Jason Silberman
Jason Silberman

Reputation: 2491

How to create a UIDatePicker on the bottom of the screen and collect the data?

This is two questions..

  1. How do I place my UIDatePicker on the bottom of the page?
  2. How can I get the date from the picker.

Here is my code for when I add the date picker:

picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[picker addTarget:self action:@selector(customDate:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:picker];

and here is my customDate: function

- (void)customDate:(id)sender {

}

so... what do I do? How do I do this?

Upvotes: 0

Views: 1439

Answers (1)

JeffN
JeffN

Reputation: 1605

Should look something like this:

- (void) customDate:(id)sender{
    NSLocale *usLocale = [[NSLocale alloc]
        initWithLocaleIdentifier:@"en_US"];

    NSDate *pickerDate = [picker date];
    NSString *selectionString = [[NSString alloc] initWithFormat:@"%@",
         [pickerDate descriptionWithLocale:usLocale]];
    //do something with the date string
}

Put what you need to do in there with the string, whether it be set a label or whatever.

And to set the frame:

picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 320, 100)];

The numbers there signify the location and size of the date picker, so it goes (x location, y location, width, height)

Upvotes: 1

Related Questions