Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone SDK: How to implement a modal date picker?

Tags:

iphone

How can I implement a modal date picker?

What I want to happen is when the user enters a text field, a new view is created that has the date picker on it. From there, the user can interact and select a date. When the view is closed it should somehow return the date to it's caller. Is this possible?

BTW, I cannot put the date picker on my main form because of space.

Thanks in advance.

like image 278
jp chance Avatar asked Oct 26 '09 13:10

jp chance


2 Answers

Use a delegate!

Header:

@class DatePickerController;
@protocol DatePickerControllerDelegate
- (void) datePickerController:(DatePickerController *)controller didPickDate:(NSDate *)date;
@end

@interface DatePickerController : UIViewController {
  UIDatePicker *datePicker;
  NSObject <DatePickerControllerDelegate> *delegate;
}

@property (nonatomic, retain) UIDatePicker *datePicker;
@property (nonatomic, assign) NSObject <DatePickerControllerDelegate> *delegate;
@end

class:

@implementation DatePickerController
- (void) loadView {
  self.view = [[[UIView alloc] init] autorelease];
  self.datePicker = [[[UIDatePicker alloc] init] autorelease];
  [self.view addSubview:self.datePicker];
  UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  [button setTitle:@"Done" forState:UIControlStateNormal];
  button.center = CGPointMake(160,240);
  [button addTarget:self action:@selector(done) forControlEvents:(UIControlEventTouchUpInside)];
  [self.view addSubview:button];
}

- (void) done {
  [delegate datePickerController:self didPickDate:datePicker.date];
}

- (void) dealloc {
  [datePicker release];
  [super dealloc];
}

@end

All that crap in loadView can be replaced with a NIB if you prefer. Just make sure to declare datePicker as an IBOutlet. And when you want to actually use it:

- (void) pickDate {
  DatePickerController *screen = [[[DatePickerController alloc] init] autorelease];
  screen.delegate = self;
  [self presentModalViewController:screen animated:YES];
}

- (void) datePickerController:(DatePickerController *)controller didPickDate:(NSDate *)date {
  [self doSomethingWithDate:date];
  [controller dismissModalViewControllerAnimated:YES];
}
like image 114
Ed Marty Avatar answered Nov 18 '22 17:11

Ed Marty


Simpler working solution:

In your .h:

@property (nonatomic,strong) IBOutlet UIDatePicker *datePicker;
@property (weak, nonatomic) IBOutlet UITextField *historyDateSelect;
@property (nonatomic, retain) UIToolbar *dateToolbar;

In your .m:

- (void)viewDidLoad
{
    [super viewDidLoad];


    self.datePicker = [[UIDatePicker alloc]init];
    [self.datePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged];


    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil action:nil];    
    [toolbar setItems:[NSArray arrayWithObject:doneButton]];

    self.dateToolbar = toolbar;

    self.historyDateSelect.inputView = self.datePicker;
    self.historyDateSelect.inputAccessoryView = self.dateToolbar;
}

And that's it. (I tested it locally and works)

like image 2
JohnMerlino Avatar answered Nov 18 '22 17:11

JohnMerlino