#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:
Output 1 in iPhone:
Output 2 in iPhone:
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:
To move to another date we click on the right arrow.
Output 4 in iPhone:
To set an event we choose a particular date and click on the event button.
Output 5 in iPhone:
Choose an event category from it.
Output 6 in iPhone:
Now save an event title or detail.
Output 7 in iPhone:
Click on Save Button to save it on memory.
App icon in iPhone:
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.