Calendar in iPhone

Introduction

In this article I will create an Empty View application. Here I will implement a Calendar programmatically, in other words via code. In this app is a new concept, the Bar Button. Here I use two bar buttons (one for clear an event and one to add an event) to perform an action of an event button. Here I use a table view controller class and UIView controller class from cocoa touch -> Objective-C class. When we click on an event button it moves to the table view controller class where I add an event list. Select any event from it and move to the UIView Controller Class. Here we add an event title or description and save it to the phone's memory.

To understand it we use the following.

Step 1

Click on the project and select Summary.

Step 2

Add an app icon or launch image.

Step 3

Now we add a Table view controller class, Nsobject type Objective-C class or some uiview controller class from Cocoa Touch.

Step 4

Here we add Main window.Xib from Resources and make it's connection.
Main-nib-base-file-name-in-iPhone.png

Step 5

Now we write the code for each class.

AppDelegate.h

//
// CalendarAppDelegate.h
// CalendarApp
//
// Created by Sachin Bhardwaj on 27/12/12.
// Copyright (c) 2012 Sachin Bhardwaj. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "CalendarViewControllerDelegate.h"

@interface CalendarAppDelegate : NSObject<UIApplicationDelegate, CalendarViewControllerDelegate> {
UIWindow *window;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@end

AppDelegate.m

//
// CalendarAppDelegate.m
// CalendarApp
//
// Created by Sachin Bhardwaj on 27/12/12.
// Copyright (c) 2012 Sachin Bhardwaj. All rights reserved.
//

#import "CalendarAppDelegate.h"
#import "CalendarViewController.h"
@implementation CalendarAppDelegate
@synthesize window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
sleep(5);
UINavigationController *navigation = [[UINavigationController alloc] init];
CalendarViewController *controller = [[CalendarViewController alloc] init];
[navigation pushViewController:controller animated:NO];
[controller setCalendarViewControllerDelegate:self];

// Override point for customization after app launch
[window addSubview:navigation.view];
[window makeKeyAndVisible];

return YES;
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{

exit(0);
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)calendarViewController:(CalendarViewController *)aCalendarViewController dateDidChange:(NSDate *)aDate {
NSLog(@"Date set to: %@", aDate);
}

- (void)dealloc {
[window release];
[super dealloc];
}
@end


CalendarLogic.h

#import <Foundation/Foundation.h>
#import "CalendarLogicDelegate.h"

@interface CalendarLogic : NSObject {
id <CalendarLogicDelegate> calendarLogicDelegate;
NSDate *referenceDate;
}

@property (nonatomic, assign) id <CalendarLogicDelegate> calendarLogicDelegate;
@property (nonatomic, retain) NSDate *referenceDate;

- (id)initWithDelegate:(id <CalendarLogicDelegate>)aDelegate referenceDate:(NSDate *)aDate;

+ (NSDate *)dateForToday;
+ (NSDate *)dateForWeekday:(NSInteger)aWeekday
onWeek:(NSInteger)aWeek
ofMonth:(NSInteger)aMonth
ofYear:(NSInteger)aYear;
+ (NSDate *)dateForWeekday:(NSInteger)aWeekday onWeek:(NSInteger)aWeek referenceDate:(NSDate *)aReferenceDate;

- (NSInteger)indexOfCalendarDate:(NSDate *)aDate;
- (NSInteger)distanceOfDateFromCurrentMonth:(NSDate *)aDate;

- (void)selectPreviousMonth;
- (void)selectNextMonth;

@end
 

CalendarLogic.m

#import "CalendarLogic.h"

@implementation CalendarLogic

#pragma mark -
#pragma mark Getters / setters

@synthesize calendarLogicDelegate;
@synthesize referenceDate;
- (void)setReferenceDate:(NSDate *)aDate {
if (aDate == nil) {
[calendarLogicDelegate calendarLogic:self dateSelected:nil];
return;
}

// Calculate direction of month switches
NSInteger distance = [self distanceOfDateFromCurrentMonth:aDate];

[referenceDate autorelease];
NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:aDate];
referenceDate = [[[NSCalendar currentCalendar] dateFromComponents:components] retain];

// Message delegate
[calendarLogicDelegate calendarLogic:self dateSelected:aDate];

// month switch?
if (distance != 0) {
// Changed so tell delegate
[calendarLogicDelegate calendarLogic:self monthChangeDirection:distance];
}
}

#pragma mark -
#pragma mark Memory management

- (void)dealloc {
self.calendarLogicDelegate = nil;
self.referenceDate = nil;

[super dealloc];
}

#pragma mark -
#pragma mark Initialization

- (id)initWithDelegate:(id <CalendarLogicDelegate>)aDelegate referenceDate:(NSDate *)aDate {
if ((self = [super init])) {
// Initialization code
self.calendarLogicDelegate = aDelegate;

[referenceDate autorelease];
NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:aDate];
referenceDate = [[[NSCalendar currentCalendar] dateFromComponents:components] retain];
}
return self;
}

#pragma mark -
#pragma mark Date computations

+ (NSDate *)dateForToday {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:[NSDate date]];
NSDate *todayDate = [calendar dateFromComponents:components];

return todayDate;
}

+ (NSDate *)dateForWeekday:(NSInteger)aWeekday onWeek:(NSInteger)aWeek referenceDate:(NSDate *)aReferenceDate {
NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:aReferenceDate];

NSInteger aMonth = [components month];
NSInteger aYear = [components year];

return [self dateForWeekday:(NSInteger)aWeekday
onWeek:(NSInteger)aWeek
ofMonth:(NSInteger)aMonth
ofYear:(NSInteger)aYear];
}
+ (NSDate *)dateForWeekday:(NSInteger)aWeekday
onWeek:(NSInteger)aWeek
ofMonth:(NSInteger)aMonth
ofYear:(NSInteger)aYear
{
NSCalendar *calendar = [NSCalendar currentCalendar];

// Select first 'firstWeekDay' in this month
NSDateComponents *firstStartDayComponents = [[[NSDateComponents alloc] init] autorelease];
[firstStartDayComponents setMonth:aMonth];
[firstStartDayComponents setYear:aYear];
[firstStartDayComponents setWeekday:[calendar firstWeekday]];
[firstStartDayComponents setWeekdayOrdinal:1];
NSDate *firstDayDate = [calendar dateFromComponents:firstStartDayComponents];

// Grab just the day part.
firstStartDayComponents = [calendar components:NSDayCalendarUnit fromDate:firstDayDate];
NSInteger numberOfDaysInWeek = [calendar maximumRangeOfUnit:NSWeekdayCalendarUnit].length;
NSInteger firstDay = [firstStartDayComponents day] - numberOfDaysInWeek;

// Correct for day landing on the firstWeekday
if ((firstDay - 1) == -numberOfDaysInWeek) {
firstDay = 1;
}

NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setYear:aYear];
[components setMonth:aMonth];
[components setDay:(aWeek * numberOfDaysInWeek) + firstDay + (aWeekday - 1)];

return [calendar dateFromComponents:components];
}

- (NSInteger)indexOfCalendarDate:(NSDate *)aDate {
NSCalendar *calendar = [NSCalendar currentCalendar];

// Split
NSDateComponents *components = [calendar components:NSWeekdayCalendarUnit | NSMonthCalendarUnit |NSWeekCalendarUnit | NSYearCalendarUnit fromDate:aDate];


// Select this month in this year.
NSDateComponents *firstDayComponents = [[[NSDateComponents alloc] init] autorelease];
[firstDayComponents setMonth:[components month]];
[firstDayComponents setYear:[components year]];
NSDate *firstDayDate = [calendar dateFromComponents:firstDayComponents];

// Turn into week of a year.
NSDateComponents *firstWeekComponents = [calendar components:NSWeekCalendarUnit fromDate:firstDayDate];
NSInteger firstWeek = [firstWeekComponents week];
if (firstWeek > [components week]) {
firstWeek = firstWeek - 52;
}
NSInteger weekday = [components weekday];
if (weekday < (NSInteger)[calendar firstWeekday]) {
weekday = weekday + 7;
}

return (weekday + (([components week] - firstWeek) * 7)) - [calendar firstWeekday];
}
- (NSInteger)distanceOfDateFromCurrentMonth:(NSDate *)aDate {
if (aDate == nil) {
return -1;
}

NSInteger distance = 0;
NSCalendar *calendar = [NSCalendar currentCalendar];

NSDateComponents *monthComponents = [calendar components:(NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:referenceDate];
NSDate *firstDayInMonth = [calendar dateFromComponents:monthComponents];
[monthComponents setDay:[calendar rangeOfUnit:NSDayCalendarUnit
inUnit:NSMonthCalendarUnit
forDate:referenceDate].length];
NSDate *lastDayInMonth = [calendar dateFromComponents:monthComponents];

// Lower
NSInteger distanceFromFirstDay = [[calendar components:NSDayCalendarUnit fromDate:firstDayInMonth toDate:aDate options:0] day];
if (distanceFromFirstDay < 0) {
distance = distanceFromFirstDay;
}

// Greater
NSInteger distanceFromLastDay = [[calendar components:NSDayCalendarUnit fromDate:lastDayInMonth toDate:aDate options:0] day];
if (distanceFromLastDay > 0) {
distance = distanceFromLastDay;
}

return distance;
}

- (void)selectPreviousMonth {
NSCalendar *calendar = [NSCalendar currentCalendar];

NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setMonth:-1];

[referenceDate autorelease];
referenceDate = [[calendar dateByAddingComponents:components toDate:referenceDate options:0] retain];
[calendarLogicDelegate calendarLogic:self monthChangeDirection:-1];
}
- (void)selectNextMonth {
NSCalendar *calendar = [NSCalendar currentCalendar];

NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setMonth:1];

[referenceDate autorelease];
referenceDate = [[calendar dateByAddingComponents:components toDate:referenceDate options:0] retain];
[calendarLogicDelegate calendarLogic:self monthChangeDirection:1];
}

@end
 

CalendarLogicDelegate

@class CalendarLogic;

@protocol CalendarLogicDelegate

- (void)calendarLogic:(CalendarLogic *)aLogic dateSelected:(NSDate *)aDate;
- (void)calendarLogic:(CalendarLogic *)aLogic monthChangeDirection:(NSInteger)aDirection;

@end


CalendarMonth.h

#import <UIKit/UIKit.h>
@class CalendarLogic;

@interface CalendarMonth : UIView {
CalendarLogic *calendarLogic;
NSArray *datesIndex;
NSArray *buttonsIndex;

NSInteger numberOfDaysInWeek;
NSInteger selectedButton;
NSDate *selectedDate;
}

@property (nonatomic, retain) CalendarLogic *calendarLogic;
@property (nonatomic, retain) NSArray *datesIndex;
@property (nonatomic, retain) NSArray *buttonsIndex;

@property (nonatomic) NSInteger numberOfDaysInWeek;
@property (nonatomic) NSInteger selectedButton;
@property (nonatomic, retain) NSDate *selectedDate;


- (id)initWithFrame:(CGRect)frame logic:(CalendarLogic *)aLogic;

- (void)selectButtonForDate:(NSDate *)aDate;

@end
 

CalendarMonth.m

#import "CalendarMonth.h"
#import "CalendarLogic.h"

#define kCalendarDayWidth 46.0f
#define kCalendarDayHeight 44.0f

@implementation CalendarMonth

#pragma mark -
#pragma mark Getters / setters

@synthesize calendarLogic;
@synthesize datesIndex;
@synthesize buttonsIndex;
@synthesize numberOfDaysInWeek;
@synthesize selectedButton;
@synthesize selectedDate;

#pragma mark -
#pragma mark Memory management

- (void)dealloc {
self.calendarLogic = nil;
self.datesIndex = nil;
self.buttonsIndex = nil;
self.selectedDate = nil;

[super dealloc];
}

#pragma mark -
#pragma mark Initialization

// Calendar object init
- (id)initWithFrame:(CGRect)frame logic:(CalendarLogic *)aLogic {

// Size is static
NSInteger numberOfWeeks = 5;
frame.size.width = 320;
frame.size.height = ((numberOfWeeks + 1) * kCalendarDayHeight) + 60;
selectedButton = -1;

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:[NSDate date]];
NSDate *todayDate = [calendar dateFromComponents:components];

if ((self = [super initWithFrame:frame])) {
// Initialization code
self.backgroundColor = [UIColor redColor]; // Red should show up fails.
self.opaque = YES;
self.clipsToBounds = NO;
self.clearsContextBeforeDrawing = NO;

UIImageView *headerBackground = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CalendarBackground.png"]] autorelease];
[headerBackground setFrame:CGRectMake(0, 0, 320, 60)];
[self addSubview:headerBackground];

UIImageView *calendarBackground = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CalendarBackground.png"]] autorelease];
[calendarBackground setFrame:CGRectMake(0, 60, 320, (numberOfWeeks + 1) * kCalendarDayHeight)];
[self addSubview:calendarBackground];

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSArray *daySymbols = [formatter shortWeekdaySymbols];
self.numberOfDaysInWeek = [daySymbols count];

UILabel *aLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)] autorelease];
aLabel.backgroundColor = [UIColor clearColor];
aLabel.textAlignment = UITextAlignmentCenter;
aLabel.font = [UIFont boldSystemFontOfSize:20];
aLabel.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"CalendarTitleColor.png"]];
aLabel.shadowColor = [UIColor whiteColor];
aLabel.shadowOffset = CGSizeMake(0, 1);

[formatter setDateFormat:@"MMMM yyyy"];
aLabel.text = [formatter stringFromDate:aLogic.referenceDate];
[self addSubview:aLabel];

UIView *lineView = [[[UIView alloc] initWithFrame:CGRectMake(0, 59, 320, 1)] autorelease];
lineView.backgroundColor = [UIColor lightGrayColor];
[self addSubview:lineView];

// Setup weekday names
NSInteger firstWeekday = [calendar firstWeekday] - 1;
for (NSInteger aWeekday = 0; aWeekday < numberOfDaysInWeek; aWeekday ++) {
NSInteger symbolIndex = aWeekday + firstWeekday;
if (symbolIndex >= numberOfDaysInWeek) {
symbolIndex -= numberOfDaysInWeek;
}

NSString *symbol = [daySymbols objectAtIndex:symbolIndex];
CGFloat positionX = (aWeekday * kCalendarDayWidth) - 1;
CGRect aFrame = CGRectMake(positionX, 40, kCalendarDayWidth, 20);

aLabel = [[[UILabel alloc] initWithFrame:aFrame] autorelease];
aLabel.backgroundColor = [UIColor clearColor];
aLabel.textAlignment = UITextAlignmentCenter;
aLabel.text = symbol;
aLabel.textColor = [UIColor darkGrayColor];
aLabel.font = [UIFont systemFontOfSize:12];
aLabel.shadowColor = [UIColor whiteColor];
aLabel.shadowOffset = CGSizeMake(0, 1);
[self addSubview:aLabel];
}

// Build calendar buttons (6 weeks of 7 days)
NSMutableArray *aDatesIndex = [[[NSMutableArray alloc] init] autorelease];
NSMutableArray *aButtonsIndex = [[[NSMutableArray alloc] init] autorelease];

for (NSInteger aWeek = 0; aWeek <= numberOfWeeks; aWeek ++) {
CGFloat positionY = (aWeek * kCalendarDayHeight) + 60;

for (NSInteger aWeekday = 1; aWeekday <= numberOfDaysInWeek; aWeekday ++) {
CGFloat positionX = ((aWeekday - 1) * kCalendarDayWidth) - 1;
CGRect dayFrame = CGRectMake(positionX, positionY, kCalendarDayWidth, kCalendarDayHeight);
NSDate *dayDate = [CalendarLogic dateForWeekday:aWeekday
onWeek:aWeek
referenceDate:[aLogic referenceDate]];
NSDateComponents *dayComponents = [calendar
components:NSDayCalendarUnit fromDate:dayDate];

UIColor *titleColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"CalendarTitleColor.png"]];
if ([aLogic distanceOfDateFromCurrentMonth:dayDate] != 0) {
titleColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"CalendarTitleDimColor.png"]];
}

UIButton *dayButton = [UIButton buttonWithType:UIButtonTypeCustom];
dayButton.opaque = YES;
dayButton.clipsToBounds = NO;
dayButton.clearsContextBeforeDrawing = NO;
dayButton.frame = dayFrame;
dayButton.titleLabel.shadowOffset = CGSizeMake(0, 1);
dayButton.titleLabel.font = [UIFont boldSystemFontOfSize:20];
dayButton.tag = [aDatesIndex count];
dayButton.adjustsImageWhenHighlighted = NO;
dayButton.adjustsImageWhenDisabled = NO;
dayButton.showsTouchWhenHighlighted = YES;

// Normal
[dayButton setTitle:[NSString stringWithFormat:@"%d", [dayComponents day]]
forState:UIControlStateNormal];

// Selected
[dayButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[dayButton setTitleShadowColor:[UIColor grayColor] forState:UIControlStateSelected];

if ([dayDate compare:todayDate] != NSOrderedSame) {
// Normal
[dayButton setTitleColor:titleColor forState:UIControlStateNormal];
[dayButton setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
[dayButton setBackgroundImage:[UIImage imageNamed:@"CalendarDayTile.png"] forState:UIControlStateNormal];

// Selected
[dayButton setBackgroundImage:[UIImage imageNamed:@"CalendarDaySelected.png"] forState:UIControlStateSelected];

} else {
// Normal
[dayButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[dayButton setTitleShadowColor:[UIColor grayColor] forState:UIControlStateNormal];
[dayButton setBackgroundImage:[UIImage imageNamed:@"CalendarDayToday.png"] forState:UIControlStateNormal];

// Selected
[dayButton setBackgroundImage:[UIImage imageNamed:@"CalendarDayTodaySelected.png"] forState:UIControlStateSelected];
}

[dayButton addTarget:self action:@selector(dayButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:dayButton];

// Save
[aDatesIndex addObject:dayDate];
[aButtonsIndex addObject:dayButton];
}
}

// save
self.calendarLogic = aLogic;
self.datesIndex = [[aDatesIndex copy] autorelease];
self.buttonsIndex = [[aButtonsIndex copy] autorelease];
}
return self;
}

#pragma mark -
#pragma mark UI Controls

- (void)dayButtonPressed:(id)sender {
[calendarLogic setReferenceDate:[datesIndex objectAtIndex:[sender tag]]];
}
- (void)selectButtonForDate:(NSDate *)aDate {
if (selectedButton >= 0) {
NSDate *todayDate = [CalendarLogic dateForToday];
UIButton *button = [buttonsIndex objectAtIndex:selectedButton];

CGRect selectedFrame = button.frame;
if ([selectedDate compare:todayDate] != NSOrderedSame) {
selectedFrame.origin.y = selectedFrame.origin.y + 1;
selectedFrame.size.width = kCalendarDayWidth;
selectedFrame.size.height = kCalendarDayHeight;
}

button.selected = NO;
button.frame = selectedFrame;

self.selectedButton = -1;
self.selectedDate = nil;
}

if (aDate != nil) {
// Save
self.selectedButton = [calendarLogic indexOfCalendarDate:aDate];
self.selectedDate = aDate;

NSDate *todayDate = [CalendarLogic dateForToday];
UIButton *button = [buttonsIndex objectAtIndex:selectedButton];

CGRect selectedFrame = button.frame;
if ([aDate compare:todayDate] != NSOrderedSame) {
selectedFrame.origin.y = selectedFrame.origin.y - 1;
selectedFrame.size.width = kCalendarDayWidth + 1;
selectedFrame.size.height = kCalendarDayHeight + 1;
}

button.selected = YES;
button.frame = selectedFrame;
[self bringSubviewToFront:button];
}
}

@end

CalendarViewController.h

#import <UIKit/UIKit.h>

#import "CalendarLogicDelegate.h"
#import "CalendarViewControllerDelegate.h"
#import "EventtableViewController.h"

@class CalendarLogic;
@class CalendarMonth;

@interface CalendarViewController : UIViewController <CalendarLogicDelegate> {
id <CalendarViewControllerDelegate> calendarViewControllerDelegate;

CalendarLogic *calendarLogic;
CalendarMonth *calendarView;
CalendarMonth *calendarViewNew;
NSDate *selectedDate;

UIButton *leftButton;
UIButton *rightButton;
}

@property (nonatomic, assign) id <CalendarViewControllerDelegate> calendarViewControllerDelegate;

@property (nonatomic, retain) CalendarLogic *calendarLogic;
@property (nonatomic, retain) CalendarMonth *calendarView;
@property (nonatomic, retain) CalendarMonth *calendarViewNew;
@property (nonatomic, retain) NSDate *selectedDate;

@property (nonatomic, retain) UIButton *leftButton;
@property (nonatomic, retain) UIButton *rightButton;

- (void)animationMonthSlideComplete;

@end

CalendarViewController.m

#import "CalendarViewController.h"

#import "CalendarLogic.h"
#import "CalendarMonth.h"

@implementation CalendarViewController

#pragma mark -
#pragma mark Getters / setters

@synthesize calendarViewControllerDelegate;

@synthesize calendarLogic;
@synthesize calendarView;
@synthesize calendarViewNew;
@synthesize selectedDate;
- (void)setSelectedDate:(NSDate *)aDate {
[selectedDate autorelease];
selectedDate = [aDate retain];

[calendarLogic setReferenceDate:aDate];
[calendarView selectButtonForDate:aDate];
}

@synthesize leftButton;
@synthesize rightButton;



#pragma mark -
#pragma mark Memory management

- (void)dealloc {
self.calendarViewControllerDelegate = nil;

self.calendarLogic.calendarLogicDelegate = nil;
self.calendarLogic = nil;

self.calendarView = nil;
self.calendarViewNew = nil;

self.selectedDate = nil;

self.leftButton = nil;
self.rightButton = nil;

[super dealloc];
}

#pragma mark -
#pragma mark Controller initialisation

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Init
}
return self;
}

#pragma mark -
#pragma mark View delegate

- (void)viewDidLoad {
[super viewDidLoad];

self.title = NSLocalizedString(@"Calendar", @"");
self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
self.view.bounds = CGRectMake(0, 0, 320, 480);
self.view.clearsContextBeforeDrawing = NO;
self.view.opaque = YES;
self.view.clipsToBounds = NO;

NSDate *aDate = selectedDate;
if (aDate == nil) {
aDate = [CalendarLogic dateForToday];
}

CalendarLogic *aCalendarLogic = [[CalendarLogic alloc] initWithDelegate:self referenceDate:aDate];
self.calendarLogic = aCalendarLogic;
[aCalendarLogic release];

UIBarButtonItem *aClearButton = [[UIBarButtonItem alloc]
initWithTitle:NSLocalizedString(@"Clear", @"") style:UIBarButtonItemStyleBordered
target:self action:@selector(actionClearDate:)];
self.navigationItem.leftBarButtonItem = aClearButton;
[aClearButton release];

UIBarButtonItem *aEventButton = [[UIBarButtonItem alloc]
initWithTitle:NSLocalizedString(@"Event", @"") style:UIBarButtonItemStyleBordered
target:self action:@selector(setEventDate:)];
self.navigationItem.rightBarButtonItem = aEventButton;
[aEventButton release];

CalendarMonth *aCalendarView = [[CalendarMonth alloc] initWithFrame:CGRectMake(0, 0, 320, 480) logic:calendarLogic];
[aCalendarView selectButtonForDate:selectedDate];
[self.view addSubview:aCalendarView];

self.calendarView = aCalendarView;
[aCalendarView release];

UIButton *aLeftButton = [UIButton buttonWithType:UIButtonTypeCustom];
aLeftButton.frame = CGRectMake(0, 0, 60, 60);
aLeftButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 20, 20);
[aLeftButton setImage:[UIImage imageNamed:@"CalendarArrowLeft.png"] forState:UIControlStateNormal];
[aLeftButton addTarget:calendarLogic
action:@selector(selectPreviousMonth)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:aLeftButton];
self.leftButton = aLeftButton;

UIButton *aRightButton = [UIButton buttonWithType:UIButtonTypeCustom];
aRightButton.frame = CGRectMake(260, 0, 60, 60);
aRightButton.imageEdgeInsets = UIEdgeInsetsMake(0, 20, 20, 0);
[aRightButton setImage:[UIImage imageNamed:@"CalendarArrowRight.png"] forState:UIControlStateNormal];
[aRightButton addTarget:calendarLogic
action:@selector(selectNextMonth)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:aRightButton];
self.rightButton = aRightButton;
}
- (void)viewDidUnload {
self.calendarLogic.calendarLogicDelegate = nil;
self.calendarLogic = nil;

self.calendarView = nil;
self.calendarViewNew = nil;

self.selectedDate = nil;

self.leftButton = nil;
self.rightButton = nil;
}

- (CGSize)contentSizeForViewInPopoverView {
return CGSizeMake(320, 324);
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad || interfaceOrientation == UIInterfaceOrientationPortrait;
}

#pragma mark -
#pragma mark UI events

- (void)actionClearDate:(id)sender {
self.selectedDate = nil;
[calendarView selectButtonForDate:nil];

// Delegate called later.
//[calendarViewControllerDelegate calendarViewController:self dateDidChange:nil];
}

- (void)setEventDate:(id)sender {

EventtableViewController *event = [[EventtableViewController alloc]init];
[self.navigationController pushViewController:event animated:YES];

}

#pragma mark -
#pragma mark CalendarLogic delegate

- (void)calendarLogic:(CalendarLogic *)aLogic dateSelected:(NSDate *)aDate {
[selectedDate autorelease];
selectedDate = [aDate retain];

if ([calendarLogic distanceOfDateFromCurrentMonth:selectedDate] == 0) {
[calendarView selectButtonForDate:selectedDate];
}

[calendarViewControllerDelegate calendarViewController:self dateDidChange:aDate];
}
- (void)calendarLogic:(CalendarLogic *)aLogic monthChangeDirection:(NSInteger)aDirection {
BOOL animate = self.isViewLoaded;

CGFloat distance = 320;
if (aDirection < 0) {
distance = -distance;
}

leftButton.userInteractionEnabled = NO;
rightButton.userInteractionEnabled = NO;

CalendarMonth *aCalendarView = [[CalendarMonth alloc] initWithFrame:CGRectMake(distance, 0, 320, 308) logic:aLogic];
aCalendarView.userInteractionEnabled = NO;
if ([calendarLogic distanceOfDateFromCurrentMonth:selectedDate] == 0) {
[aCalendarView selectButtonForDate:selectedDate];
}
[self.view insertSubview:aCalendarView belowSubview:calendarView];

self.calendarViewNew = aCalendarView;
[aCalendarView release];

if (animate) {
[UIView beginAnimations:NULL context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationMonthSlideComplete)];
[UIView setAnimationDuration:0.3];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
}

calendarView.frame = CGRectOffset(calendarView.frame, -distance, 0);
aCalendarView.frame = CGRectOffset(aCalendarView.frame, -distance, 0);

if (animate) {
[UIView commitAnimations];

} else {
[self animationMonthSlideComplete];
}
}

- (void)animationMonthSlideComplete {
// Get rid of the old one.
[calendarView removeFromSuperview];

// replace
self.calendarView = calendarViewNew;
self.calendarViewNew = nil;

leftButton.userInteractionEnabled = YES;
rightButton.userInteractionEnabled = YES;
calendarView.userInteractionEnabled = YES;
}

@end

DetailsViewController.h

#import <UIKit/UIKit.h>

@interface DetailsViewController : UIViewController{
IBOutlet UILabel *titlelbl;
IBOutlet UILabel *detailslabel;
IBOutlet UITextField *titletxt;
IBOutlet UITextField *Detaistxt;
IBOutlet UIButton *savebtn;
}

@property (nonatomic,strong) IBOutlet UILabel *titlelbl;
@property (nonatomic,strong) UILabel *detailslabel;
@property (nonatomic,strong) UITextField *titletxt;
@property (nonatomic,strong) IBOutlet UITextField *Detaistxt;
@property (nonatomic,strong) IBOutlet UIButton *savebtn;
@end

DetailsViewController.m

#import "DetailsViewController.h"

@interface DetailsViewController ()
@end

@implementation DetailsViewController
@synthesize detailslabel,Detaistxt,savebtn,titlelbl,titletxt;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {

[textField resignFirstResponder];

return YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

[Detaistxt resignFirstResponder];
[titletxt resignFirstResponder];

}

@end


EventtableViewController.h

#import <UIKit/UIKit.h>

@interface EventtableViewController : UITableViewController
{
IBOutlet UITableView *tabel;
NSMutableArray *array;
}
@property(strong,nonatomic)IBOutlet UITableView *tabel;

@end

EventtableViewController.m

#import "EventtableViewController.h"
#import "DetailsViewController.h"

@interface EventtableViewController ()

@end


@implementation EventtableViewController
@synthesize tabel;

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

array = [[NSMutableArray alloc]init];


[array addObject:@"Appointment"];
[array addObject:@"Anniversary"];
[array addObject:@"BirthDay"];
[array addObject:@"Holiday"];
[array addObject:@"Important"];
[array addObject:@"Privet"];}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

return [array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

cell.textLabel.text = [array objectAtIndex:indexPath.row];

return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
DetailsViewController *Detail = [[DetailsViewController alloc]init];
[self.navigationController pushViewController:Detail animated:YES];

}

@end


Step 6

Finally we click on the Run button to show the output.


Splash Screen in iPhone:

splash-in-iPhone.jpg

Output 1 in iPhone:

output1-in-iPhone.jpg

Output 2 in iPhone:

output2-in-iPhone.jpg

To clear an event detail  from a
particular date, we select those dates and click on the clear button; it removes all details that were saved on it before .

Output 3 in iPhone:

output3-in-iPhone.jpg
 

To move to another date we click on the right arrow.

Output 4 in iPhone:

output4-in-iPhone.jpg

To set an event we choose a particular date and click on the event button.

Output 5 in iPhone:

output5-in-iPhone.jpg
 

Choose an event category from it.

Output 6 in iPhone:

output6-in-iPhone.jpg
 

Now save an event title or detail.

Output 7 in iPhone:

output7-in-iPhone.jpg
 

Click on Save Button to save it on memory.

App icon in iPhone:

 appicon-in-iPhone.jpg

After running the app, click on the iPhone home button. We will see an app icon, click on it to run it again and enjoy the New Year 2013.


Similar Articles