Friday, January 23, 2009

A Better Generic Date Picker

I've tweaked my generic date picker class so it matches, pixel-for-pixel, the date picker used by Apple's built-in apps like the Address Book (Contacts.app), and also fixed some bugs in the previous version.



Here is the code that implements it. Use is exactly the same as with the previous version.

Note: if you copied this before about 5:30pm EST, Friday January 23, 2009, you might want to re-copy it. There was a memory leak thanks to me forgetting to release the the date formatter instance.

DateViewController.h
/*
DateViewController.h
*/


#import <UIKit/UIKit.h>

@protocol DateViewDelegate <NSObject>
@required
- (void)takeNewDate:(NSDate *)newDate;
- (UINavigationController *)navController; // Return the navigation controller
@end


@interface DateViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
UIDatePicker *datePicker;
UITableView *dateTableView;
NSDate *date;

id <DateViewDelegate> delegate; // weak ref
}
@property (nonatomic, retain) UIDatePicker *datePicker;
@property (nonatomic, retain) UITableView *dateTableView;
@property (nonatomic, retain) NSDate *date;
@property (nonatomic, assign) id <DateViewDelegate> delegate;
-(IBAction)dateChanged;
@end


DateViewController.m
/*
DateViewController.m
*/

#import "DateViewController.h"

@implementation DateViewController
@synthesize datePicker;
@synthesize dateTableView;
@synthesize date;
@synthesize delegate;

-(IBAction)dateChanged
{
self.date = [datePicker date];
[dateTableView reloadData];
}
-(IBAction)cancel
{
[[self.delegate navController] popViewControllerAnimated:YES];
}
-(IBAction)save
{
[self.delegate takeNewDate:date];
[[self.delegate navController] popViewControllerAnimated:YES];
}
- (void)loadView
{
UIView *theView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.view = theView;
[theView release];

UITableView *theTableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 67.0, 320.0, 480.0) style:UITableViewStyleGrouped];
theTableView.delegate = self;
theTableView.dataSource = self;
[self.view addSubview:theTableView];
self.dateTableView = theTableView;
[theTableView release];

UIDatePicker *theDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 200.0, 320.0, 216.0)];
theDatePicker.datePickerMode = UIDatePickerModeDate;
self.datePicker = theDatePicker;
[theDatePicker release];
[datePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];




UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]
initWithTitle:NSLocalizedString(@"Cancel", @"Cancel - for button to cancel changes")
style:UIBarButtonItemStylePlain
target:self
action:@selector(cancel)]
;
self.navigationItem.leftBarButtonItem = cancelButton;
[cancelButton release];
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
initWithTitle:NSLocalizedString(@"Save", @"Save - for button to save changes")
style:UIBarButtonItemStylePlain
target:self
action:@selector(save)]
;
self.navigationItem.rightBarButtonItem = saveButton;
[saveButton release];

self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];

}

- (void)viewWillAppear:(BOOL)animated
{
if (self.date != nil)
[self.datePicker setDate:date animated:YES];
else
[self.datePicker setDate:[NSDate date] animated:YES];

[super viewWillAppear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc
{
[datePicker release];
[dateTableView release];
[date release];
[super dealloc];
}
#pragma mark -
#pragma mark Table View Methods
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *DateCellIdentifier = @"DateCellIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DateCellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:DateCellIdentifier] autorelease];
cell.font = [UIFont systemFontOfSize:17.0];
cell.textColor = [UIColor colorWithRed:0.243 green:0.306 blue:0.435 alpha:1.0];
}

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMMM dd, yyyy"];
cell.text = [formatter stringFromDate:date];
[formatter release];


return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
@end




15 comments:

Jeff (composer) said...

I assume you also want to add:

[formatter release];

after the cell.text assignment in tableView:cellForRowAtIndexPath, so you don't have a small leak.

Otherwise, it's always nice to see good code.

Jeff LaMarche said...

Absolutely. Good catch, and thanks. I'll get it updated shortly.

Jesse said...

When setting the delegate of the date controller to self I'm getting a warning that my view controller does not implement the DateViewDelegate. When I select a date the delegate method in my view controller isn't getting called and the app crashes. I'm working with 2.2 if that makes a difference. Thanks for your time and help creating these reusable controls!

Jesse said...

I figured out my problem. I hadn't set the navController variable in my delegate.

Allergic2Rhinos said...

You might be able to improve the support for region specific date formats.

The address book date picker, for example, will show the date picker wheels in a different order if the region is set to Canada rather than US.

The date label, above the wheels, should probably reflect that same ordering. I'd suggest using one of the standard date formatting constants and hopefully it will match the wheel order for most regions.

Hiram Fernandez said...

Jesse,

How do you setup the navController variable in the delegate.

cheryl said...

Thanks for putting this page together it helpe me alot. I have a problem though when I am viewing the page. As the date picker is on the table when I select to scroll the dates is scrolls the table. My view is within a navigation controller and has been pushed into the stack - does that make a difference?

Any help would be very gratefully appreciated.

Nathan J said...

Jeff - great tutorial and everything seems to be working except for the fact that the navigation bar will not display. I've attempted tweaking the frames and even played with the hidden setting but nothing seems to work. Any thoughts?

Thanks!

Nathan J said...

My issue was related to the fact that I was presenting the view as modal.

Solution: Throw the view into a navigation controller container and present the nav controller as the modal view.

Edwin said...

scrub m65 kamagra attorney lawyer body scrub field jacket lovegra marijuana attorney injury lawyer

Jack Meoffa said...

Does this validate the date to ensure it is a correct date, for example no 31 Feb, proper handling of leap years, etc?

vibhor goyal said...

Thanks a lot! It worked really well!

Joshua said...

Very helpful; thank you!

h4ns said...

What youre saying is completely true. I know that everybody must say the same thing, but I just think that you put it in a way that everyone can understand. I also love the images you put in here. They fit so well with what youre trying to say. Im sure youll reach so many people with what youve got to say.

Arsenal vs Huddersfield Town live streaming
Arsenal vs Huddersfield Town live streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Notts County vs Manchester City Live Streaming
Notts County vs Manchester City Live Streaming
Bologna vs AS Roma Live Streaming
Bologna vs AS Roma Live Streaming
Juventus vs Udinese Live Streaming
Juventus vs Udinese Live Streaming
Napoli vs Sampdoria Live Streaming
Napoli vs Sampdoria Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
AS Monaco vs Marseille Live Streaming
AS Monaco vs Marseille Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Technology News | News Today | Live Streaming TV Channels

chillbo said...

hi,
I appreciate these tutorials but I'm having trouble understanding portions of the loadview method. 
Why are we adding the instance of the class object as a subview prior to making the dateTableView property = theTableView?

[self.view addSubview:theTableView];
self.dateTableView = theTableView;

Why can't we do this...
self.dateTableView = theTableView
[self.view addSubview:dateTableView]

which is what we see with the datePicker portion of the method...

self.datePicker = theDatePicker;
[theDatePicker release];
[datePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];

Any help clarifying this would be appreciated. Thanks a lot.