Cocoa Touch: iPhone Calendar Month View Class
August 12th, 2009CONTENTS OF ARTICLE ARE OUTDATED.
You won’t find that cool calendar view class you see in the Calendar app in the iPhone 3.0 SDK, but you will find it in the open source Tapku Library. The tapku library calendar view looks (almost) exactly like the one you see in the Calendar app and it acts the same way with the ability to add an indicator for each day that has some piece of information associated with it. The rest of article will step you through using the TKCalendarView and TKCalendarViewController.
The easiest way to use the calendar view is to use the TKCalendarViewController and implement the following Calendar View delegate methods.
- (NSArray*) calendarView:(TKCalendarView*)calendar itemsForDaysInMonth:(NSDate*)monthDate; - (void) calendarView:(TKCalendarView*)calendar dateWasSelected:(NSInteger)integer ofMonth:(NSDate*)monthDate; - (void) calendarView:(TKCalendarView*)calendar willShowMonth:(NSDate*)monthDate;
The first delegate method, the one that expects back an NSArray, is the function that sets up the marks for each day in the month. The monthDate variable is the time-stamp for the first day in that particular month requested, like for instance August 1st 2009 0:00:00.0. The array returned should be filled with 31,30 or 28 (however many days are in that particular month) NSNumber objects with boolean values. In terms of programming, this function is a good time to query a database, and store the objects in memory so you can easily display information when a day is selected.
The dateWasSelected method is fired by the Calendar view every time the user selects a new day or they switch months. The monthDate variable again is the time-stamp day for the first of the month and the integer variable is the actual day of that month. You may wonder why this isn’t another date variable. This is just to keep the memory and processor use down as well as make it easier to code on the controller side.
The willShowMonth method will fire when months are swapped in and out. This method is useful when you want to adjust objects in the view because of the resizing of the calendar. Recall, the Calendar app has a UITableView stuck to the bottom of the Calendar view. When the calendar switches months and calls that delegate method, you can resize something like a table view to the new frame height of the calendar view.
To use the Calendar View, download the Tapku Library from github and make sure to check out view and view controller classes as well as the demo app to see the class in action.

