Calendar View Control for iPhone
2. In the Classes folder add my NRGUICalendarView.h and NRGUICalendarView.m files.
#import <UIKit/UIKit.h>
@class NRGUICalendarView;
@interface CalTestViewController : UIViewController {
NRGUICalendarView *cal;
IBOutlet UILabel *monthLabel;
IBOutlet UILabel *yearLabel;
IBOutlet UIView *calCont;
}@property (nonatomic, retain) IBOutlet UILabel *monthLabel;
@property (nonatomic, retain) IBOutlet UILabel *yearLabel;
@property (nonatomic, retain) IBOutlet UIView *calCont;- (IBAction)prevMonth:(id)sender;
- (IBAction)nextMonth:(id)sender;
- (IBAction)prevYear:(id)sender;
- (IBAction)nextYear:(id)sender;@end
4. Now add synthesis and dealloc code to the CalTestViewController.h file.
#import "CalTestViewController.h"
#import "NRGUICalendarView.h"@implementation CalTestViewController
@synthesize yearLabel, monthLabel, calCont;
————————————————–
————————————————–- (void)dealloc {
[yearLabel release];
[monthLabel release];
[calCont release];
[super dealloc];
}
5. Now open CalTestViewController.xib in interface builder and add four buttons, two labels and a view to the root view as shown below and the connect all outlets and assign all actions accordingly.
IBAction)prevMonth:(id)sender {
[cal previousMonth];
monthLabel.text = [cal currentMonthName];
}- (IBAction)nextMonth:(id)sender {
[cal nextMonth];
monthLabel.text = [cal currentMonthName];
}- (IBAction)prevYear:(id)sender {
[cal previousYear];
yearLabel.text = [[NSString alloc] initWithFormat:@"%i", [cal currentYear]];
}- (IBAction)nextYear:(id)sender {
[cal nextYear];
yearLabel.text = [[NSString alloc] initWithFormat:@"%i", [cal currentYear]];
}
cal = [[NRGUICalendarView alloc] initWithHandler:self];
[calCont addSubview:cal];
monthLabel.text = [cal currentMonthName];
yearLabel.text = [[NSString alloc] initWithFormat:@"%i", [cal currentYear]];
8. Now we need to tell the calendar control that we are going to use the CalTestViewController as its data source and delegate. That’s why we passed self to the initWithHandler message. Now add the following two methods to CalTestViewController.
- (void)daySelected:(id)sender {
}- (BOOL)isMarked:(NSInteger)year month:(NSInteger)month day:(NSInteger)day {
return NO;
}
9. Now build and run the simulator and you should see something like the following.
- (void)renderYear:(NSInteger)year month:(NSInteger)month;
renders the calendar view to show the specified month.
- (void)gotoYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day;
selects the specified cell by day, month, year.
- (void)goToday;
goes and selects the current day.
- (void)previousMonth;
navigates to previous month.
- (void)nextMonth;
navigates to next month.
- (void)previousYear;
navigates to previous year.
- (void)nextYear;
navigates to next year.
returns current month name as string.
- (NSInteger)currentMonth;
returns current month as integer (January = 1, February = 2, and so on).
- (NSInteger)currentYear;
returns current year as integer.




