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, t
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)