Skip to content

Commit

Permalink
KalViewController's designated initializer is now 'init'. HolidaysDem…
Browse files Browse the repository at this point in the history
…o now shows how to push a details view controller when the user taps a holiday listed below the calendar.
  • Loading branch information
klazuka committed Jan 12, 2010
1 parent a640eef commit 8fa33f4
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 37 deletions.
8 changes: 7 additions & 1 deletion HolidaysDemo/HolidayAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@
* the data to the user in the same way (via UITableViewCells).
* The only difference is in the way that they retrieve the data.
*
* HolidayAppDelegate implements the UITableViewDelegate protocol
* so that it can respond to the user tapping the name of a holiday
* below the calendar by pushing a details view controller onto
* the navigation stack.
*
*/
@interface HolidayAppDelegate : NSObject <UIApplicationDelegate>
@interface HolidayAppDelegate : NSObject <UIApplicationDelegate, UITableViewDelegate>
{
UIWindow *window;
UINavigationController *navController;
id dataSource;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
Expand Down
24 changes: 21 additions & 3 deletions HolidaysDemo/HolidayAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#import "HolidayAppDelegate.h"
#import "HolidayJSONDataSource.h"
#import "HolidaySqliteDataSource.h"
#import "HolidaysDetailViewController.h"
#import "Kal.h"

@implementation HolidayAppDelegate
Expand All @@ -15,17 +16,34 @@ @implementation HolidayAppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
// I provide several different dataSource examples. Pick one by commenting out the others.
// KalViewController *kal = [[KalViewController alloc] initWithDataSource:[HolidayJSONDataSource dataSource]];
KalViewController *kal = [[KalViewController alloc] initWithDataSource:[HolidaySqliteDataSource dataSource]];
navController = [[UINavigationController alloc] initWithRootViewController:kal];
dataSource = [[HolidayJSONDataSource alloc] init];
// dataSource = [[HolidaySqliteDataSource alloc] init];
KalViewController *kal = [[KalViewController alloc] init];
kal.delegate = self;
kal.dataSource = dataSource;
kal.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Today" style:UIBarButtonItemStyleBordered target:kal action:@selector(showAndSelectToday)] autorelease];
navController = [[UINavigationController alloc] initWithRootViewController:kal];
[kal release];

[window addSubview:navController.view];
[window makeKeyAndVisible];
}

#pragma mark UITableViewDelegate protocol conformance

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Holiday *holiday = [dataSource holidayAtIndexPath:indexPath];
HolidaysDetailViewController *vc = [[HolidaysDetailViewController alloc] initWithHoliday:holiday];
[navController pushViewController:vc animated:YES];
[vc release];
}

#pragma mark -

- (void)dealloc
{
[dataSource release];
[window release];
[navController release];
[super dealloc];
Expand Down
3 changes: 3 additions & 0 deletions HolidaysDemo/HolidayJSONDataSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#import "Kal.h"

@class Holiday;

/*
* HolidayJSONDataSource
* ---------------------
Expand All @@ -25,5 +27,6 @@
}

+ (HolidayJSONDataSource *)dataSource;
- (Holiday *)holidayAtIndexPath:(NSIndexPath *)indexPath; // exposed for HolidayAppDelegate so that it can implement the UITableViewDelegate protocol.

@end
7 changes: 6 additions & 1 deletion HolidaysDemo/HolidayJSONDataSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ - (id)init
return self;
}

- (Holiday *)holidayAtIndexPath:(NSIndexPath *)indexPath
{
return [items objectAtIndex:indexPath.row];
}

#pragma mark UITableViewDataSource protocol conformance

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Expand All @@ -47,7 +52,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
}

Holiday *holiday = [items objectAtIndex:indexPath.row];
Holiday *holiday = [self holidayAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"flags/%@.gif", holiday.country]];
cell.textLabel.text = holiday.name;
return cell;
Expand Down
3 changes: 3 additions & 0 deletions HolidaysDemo/HolidaySqliteDataSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#import "Kal.h"

@class Holiday;

/*
* HolidaySqliteDataSource
* ---------------------
Expand All @@ -23,5 +25,6 @@
}

+ (HolidaySqliteDataSource *)dataSource;
- (Holiday *)holidayAtIndexPath:(NSIndexPath *)indexPath; // exposed for HolidayAppDelegate so that it can implement the UITableViewDelegate protocol.

@end
7 changes: 6 additions & 1 deletion HolidaysDemo/HolidaySqliteDataSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ - (id)init
return self;
}

- (Holiday *)holidayAtIndexPath:(NSIndexPath *)indexPath
{
return [items objectAtIndex:indexPath.row];
}

#pragma mark UITableViewDataSource protocol conformance

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Expand All @@ -45,7 +50,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
}

Holiday *holiday = [items objectAtIndex:indexPath.row];
Holiday *holiday = [self holidayAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"flags/%@.gif", holiday.country]];
cell.textLabel.text = holiday.name;
return cell;
Expand Down
22 changes: 22 additions & 0 deletions HolidaysDemo/HolidaysDetailViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2009 Keith Lazuka
* License: http://www.opensource.org/licenses/mit-license.html
*/

@class Holiday;

/*
* HolidaysDetailViewController
* ----------------------------
*
* This view controller will be pushed onto the navigation stack
* when the user taps the row for a holiday beneath the calendar.
*/
@interface HolidaysDetailViewController : UIViewController
{
Holiday *holiday;
}

- (id)initWithHoliday:(Holiday *)holiday;

@end
35 changes: 35 additions & 0 deletions HolidaysDemo/HolidaysDetailViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2009 Keith Lazuka
* License: http://www.opensource.org/licenses/mit-license.html
*/

#import "HolidaysDetailViewController.h"
#import "Holiday.h"

@implementation HolidaysDetailViewController

- (id)initWithHoliday:(Holiday *)aHoliday
{
if ((self = [super init])) {
holiday = [aHoliday retain];
}
return self;
}

- (void)loadView
{
UILabel *label = [[UILabel alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
label.text = [NSString stringWithFormat:@"%@ - %@", holiday.country, holiday.name];
label.textAlignment = UITextAlignmentCenter;
self.view = label;
[label release];
}

- (void)dealloc
{
[holiday release];
[super dealloc];
}


@end
6 changes: 6 additions & 0 deletions Kal.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
5387C0E810EE389A0011C4BA /* HolidaySqliteDataSource.m in Sources */ = {isa = PBXBuildFile; fileRef = 5387C0E710EE389A0011C4BA /* HolidaySqliteDataSource.m */; };
5387C10B10EE3D560011C4BA /* holidays.db in Resources */ = {isa = PBXBuildFile; fileRef = 5387C10A10EE3D560011C4BA /* holidays.db */; };
5387C17310EEC7100011C4BA /* flags in Resources */ = {isa = PBXBuildFile; fileRef = 5387C16810EEC7100011C4BA /* flags */; };
53C660B310FCBB8C00A12D4F /* HolidaysDetailViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 53C660B210FCBB8C00A12D4F /* HolidaysDetailViewController.m */; };
53D9F86810DD9CC800759172 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 53D9F86310DD9CC800759172 /* main.m */; };
53D9F86910DD9CC800759172 /* MainWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 53D9F86410DD9CC800759172 /* MainWindow.xib */; };
53D9F88C10DD9CEE00759172 /* KalDataSource.m in Sources */ = {isa = PBXBuildFile; fileRef = 53D9F86D10DD9CEE00759172 /* KalDataSource.m */; };
Expand Down Expand Up @@ -94,6 +95,8 @@
5387C0E710EE389A0011C4BA /* HolidaySqliteDataSource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HolidaySqliteDataSource.m; sourceTree = "<group>"; };
5387C10A10EE3D560011C4BA /* holidays.db */ = {isa = PBXFileReference; lastKnownFileType = file; path = holidays.db; sourceTree = "<group>"; };
5387C16810EEC7100011C4BA /* flags */ = {isa = PBXFileReference; lastKnownFileType = folder; path = flags; sourceTree = "<group>"; };
53C660B110FCBB8C00A12D4F /* HolidaysDetailViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HolidaysDetailViewController.h; sourceTree = "<group>"; };
53C660B210FCBB8C00A12D4F /* HolidaysDetailViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HolidaysDetailViewController.m; sourceTree = "<group>"; };
53D9F86310DD9CC800759172 /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
53D9F86410DD9CC800759172 /* MainWindow.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = MainWindow.xib; sourceTree = "<group>"; };
53D9F86C10DD9CEE00759172 /* KalDataSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KalDataSource.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -201,6 +204,8 @@
5387C10A10EE3D560011C4BA /* holidays.db */,
53D9F8AB10DD9D9D00759172 /* HolidayAppDelegate.h */,
53D9F8AC10DD9D9D00759172 /* HolidayAppDelegate.m */,
53C660B110FCBB8C00A12D4F /* HolidaysDetailViewController.h */,
53C660B210FCBB8C00A12D4F /* HolidaysDetailViewController.m */,
5387BCFB10EBB1960011C4BA /* Holiday.h */,
5387BCFC10EBB1960011C4BA /* Holiday.m */,
53D9F86310DD9CC800759172 /* main.m */,
Expand Down Expand Up @@ -370,6 +375,7 @@
5387BFA110ECEE570011C4BA /* SBJsonWriter.m in Sources */,
5387C0DF10EE38440011C4BA /* HolidayJSONDataSource.m in Sources */,
5387C0E810EE389A0011C4BA /* HolidaySqliteDataSource.m in Sources */,
53C660B310FCBB8C00A12D4F /* HolidaysDetailViewController.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
7 changes: 4 additions & 3 deletions Kal/KalViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
{
KalLogic *logic;
UITableView *tableView;
id <UITableViewDelegate> delegate;
id <KalDataSource> dataSource;
}

@property (nonatomic, readonly) KalView *calendarView;
@property (nonatomic, readonly) UITableView *tableView;
@property (nonatomic, assign) id<UITableViewDelegate> delegate;
@property (nonatomic, assign) id<KalDataSource> dataSource;

- (id)initWithDataSource:(id<KalDataSource>)source; // designated initializer
- (void)reloadData; // If you change the KalDataSource after the KalViewController has already been displayed to the user, you must call this method in order for the view to reflect the new data.
- (void)showAndSelectToday; // Updates the state of the calendar to display today's month and selects the tile for today's date.

@end
58 changes: 30 additions & 28 deletions Kal/KalViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,43 +34,41 @@ - (KalView*)calendarView;

@implementation KalViewController

- (id)initWithDataSource:(id<KalDataSource>)source
@synthesize dataSource, delegate;

- (KalView*)calendarView { return (KalView*)self.view; }

- (void)setDataSource:(id<KalDataSource>)aDataSource
{
if ((self = [super init])) {
dataSource = [source retain];
if (dataSource != aDataSource) {
[dataSource release];
[aDataSource retain];
dataSource = aDataSource;
tableView.dataSource = dataSource;
}
return self;
}
}

- (id)init
- (void)setDelegate:(id<UITableViewDelegate>)aDelegate
{
return [self initWithDataSource:[SimpleKalDataSource dataSource]];
if (delegate != aDelegate) {
[delegate release];
[aDelegate retain];
delegate = aDelegate;
tableView.delegate = delegate;
}
}

- (KalView*)calendarView { return (KalView*)self.view; }

- (void)clearTable
{
[dataSource removeAllItems];
[tableView reloadData];
}

- (void)fetchDataForCurrentMonth
- (void)reloadData
{
[dataSource presentingDatesFrom:logic.fromDate to:logic.toDate delegate:self];
}

- (UITableView *)tableView
{
UITableView *table = [[self calendarView] tableView];
if (!table) {
[self loadView];
table = [[self calendarView] tableView];
}

return table;
}

// -----------------------------------------
#pragma mark KalViewDelegate protocol

Expand All @@ -89,15 +87,15 @@ - (void)showPreviousMonth
[self clearTable];
[logic retreatToPreviousMonth];
[[self calendarView] slideDown];
[self fetchDataForCurrentMonth];
[self reloadData];
}

- (void)showFollowingMonth
{
[self clearTable];
[logic advanceToFollowingMonth];
[[self calendarView] slideUp];
[self fetchDataForCurrentMonth];
[self reloadData];
}

// -----------------------------------------
Expand Down Expand Up @@ -139,7 +137,7 @@ - (void)showAndSelectToday
#endif

[[self calendarView] selectTodayIfVisible];
[self fetchDataForCurrentMonth];
[self reloadData];
}

// -----------------------------------------------------------------------------------
Expand All @@ -149,10 +147,15 @@ - (void)loadView
{
self.title = @"Calendar";
logic = [[KalLogic alloc] init];
self.view = [[[KalView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] delegate:self logic:logic] autorelease];
tableView = [[[self calendarView] tableView] retain];

KalView *kalView = [[KalView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] delegate:self logic:logic];
self.view = kalView;
tableView = kalView.tableView;
tableView.dataSource = dataSource;
[self fetchDataForCurrentMonth];
tableView.delegate = delegate;
[tableView retain];
[kalView release];
[self reloadData];
}

- (void)viewWillAppear:(BOOL)animated
Expand All @@ -173,7 +176,6 @@ - (void)dealloc
{
[logic release];
[tableView release];
[dataSource release];
[super dealloc];
}

Expand Down

0 comments on commit 8fa33f4

Please sign in to comment.