Classes UIDatePicker and NSDateFormatter

A UIDatePicker is a view that looked like a Las Vegas slot machine in iOS 6. We usually initialize a view by passing a frame rectangle to initWithFrame:, but a UIDatePicker already has a default size of 320 × 216 pairs of pixels (undocumented). We could resize it with its transform property and CGAffineTransformMakeScale, but don’t. Everyone expects that the UIDatePicker will remain its natural size. We should specify only its position.

Source code in DatePicker.zip

  1. main.m
  2. Class DatePickerAppDelegate
  3. Class View

Things to try

  1. The date picker is initialized to midnight of the current date. Initialize it to a different date by inserting the following code after the datePickerMode in initWithFrame:
    		//so we don't have to look up the day of the week of December 31, 2014
    		[dateFormatter setDateStyle: NSDateFormatterLongStyle];
    
    		//so we don't have to specify a time
    		[dateFormatter setTimeStyle: NSDateFormatterNoStyle];
    
    		datePicker.date = [dateFormatter dateFromString: @"December 31, 2014"];
    
  2. Instead of printing the date we picked, print the date on which the baby is due. In the valueChanged method of class View, change
    	textView.text = [dateFormatter stringFromDate: p.date];
    
    to
    	NSDate *dueDate = [[NSDate alloc] initWithTimeInterval: 60 * 60 * 24 * 280
    		sinceDate: p.date
    	];
    
    	textView.text = [dateFormatter stringFromDate: dueDate];
    

  3. Try all four modes:
    1. UIDatePickerModeTime
    2. UIDatePickerModeDate
    3. UIDatePickerModeDateAndTime
    4. UIDatePickerModeCountDownTimer

  4. See how it animates away from February 30? Make it animate away from any date that is a Sunday. If the date is a Sunday, assign a different value to the date property of the date picker.

  5. [Not an assignment.] Make a Jewish calendar date picker. Insert the following into the initWithFrame: method of class View immediately after creating the picker. AM is Anno Mundi.
    	datePicker.calendar =
    		[[NSCalendar alloc] initWithCalendarIdentifier: NSHebrewCalendar];
    
    	NSLog(@"picker.calendar.calendarIdentifier == %@",
    		datePicker.calendar.calendarIdentifier);
    
    2013-11-06 22:41:53.684 DatePicker[4572:a0b] picker.calendar.calendarIdentifier == hebrew