Retired Document
Important: This document has not been updated for iOS 7 or later. The content is largely covered in Start Developing iOS Apps Today (Retired).
Code Listings
This appendix contains code listings for the interface and implementation files of the BirdWatching project. The listings do not include comments or methods that you do not edit.
Model Layer Files
This section contains listings for the following files:
BirdSighting.h
BirdSighting.m
BirdSightingDataController.h
BirdSightingDataController.m
BirdSighting.h
#import <Foundation/Foundation.h> |
@interface BirdSighting : NSObject |
@property (nonatomic, copy) NSString *name; |
@property (nonatomic, copy) NSString *location; |
@property (nonatomic, strong) NSDate *date; |
-(id)initWithName:(NSString *)name location:(NSString *)location date:(NSDate *)date; |
@end |
BirdSighting.m
#import "BirdSighting.h" |
@implementation BirdSighting |
-(id)initWithName:(NSString *)name location:(NSString *)location date:(NSDate *)date |
{ |
self = [super init]; |
if (self) { |
_name = name; |
_location = location; |
_date = date; |
return self; |
} |
return nil; |
} |
@end |
BirdSightingDataController.h
#import <Foundation/Foundation.h> |
@class BirdSighting; |
@interface BirdSightingDataController : NSObject |
@property (nonatomic, copy) NSMutableArray *masterBirdSightingList; |
- (NSUInteger)countOfList; |
- (BirdSighting *)objectInListAtIndex:(NSUInteger)theIndex; |
- (void)addBirdSightingWithSighting:(BirdSighting *)sighting; |
@end |
BirdSightingDataController.m
#import "BirdSightingDataController.h" |
#import "BirdSighting.h" |
@interface BirdSightingDataController () |
- (void)initializeDefaultDataList; |
@end |
@implementation BirdSightingDataController |
- (void)initializeDefaultDataList { |
NSMutableArray *sightingList = [[NSMutableArray alloc] init]; |
self.masterBirdSightingList = sightingList; |
BirdSighting *sighting; |
NSDate *today = [NSDate date]; |
sighting = [[BirdSighting alloc] initWithName:@"Pigeon" location:@"Everywhere" date:today]; |
[self addBirdSightingWithSighting:sighting]; |
} |
- (void)setMasterBirdSightingList:(NSMutableArray *)newList { |
if (_masterBirdSightingList != newList) { |
_masterBirdSightingList = [newList mutableCopy]; |
} |
} |
- (id)init { |
if (self = [super init]) { |
[self initializeDefaultDataList]; |
return self; |
} |
return nil; |
} |
- (NSUInteger)countOfList { |
return [self.masterBirdSightingList count]; |
} |
- (BirdSighting *)objectInListAtIndex:(NSUInteger)theIndex { |
return [self.masterBirdSightingList objectAtIndex:theIndex]; |
} |
- (void)addBirdSightingWithSighting:(BirdSighting *)sighting { |
[self.masterBirdSightingList addObject:sighting]; |
} |
@end |
Master View Controller Files
This section contains listings for the following files:
BirdsMasterViewController.h
BirdsMasterViewController.m
BirdsMasterViewController.h
#import <UIKit/UIKit.h> |
@class BirdSightingDataController; |
@interface BirdsMasterViewController : UITableViewController |
@property (strong, nonatomic) BirdSightingDataController *dataController; |
- (IBAction)done:(UIStoryboardSegue *)segue; |
- (IBAction)cancel:(UIStoryboardSegue *)segue; |
@end |
BirdsMasterViewController.m
#import "BirdsMasterViewController.h" |
#import "BirdsDetailViewController.h" |
#import "BirdSightingDataController.h" |
#import "BirdSighting.h" |
#import "AddSightingViewController.h" |
@implementation BirdsMasterViewController |
- (void)awakeFromNib |
{ |
[super awakeFromNib]; |
self.dataController = [[BirdSightingDataController alloc] init]; |
} |
- (void)viewDidLoad |
{ |
[super viewDidLoad]; |
self.navigationItem.rightBarButtonItem.accessibilityHint = @"Adds a new bird sighting event"; |
// Do any additional setup after loading the view, typically from a nib. |
} |
- (void)didReceiveMemoryWarning |
{ |
[super didReceiveMemoryWarning]; |
// Dispose of any resources that can be recreated. |
} |
#pragma mark - Table View |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView |
{ |
return 1; |
} |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
return [self.dataController countOfList]; |
} |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
static NSString *CellIdentifier = @"BirdSightingCell"; |
static NSDateFormatter *formatter = nil; |
if (formatter == nil) { |
formatter = [[NSDateFormatter alloc] init]; |
[formatter setDateStyle:NSDateFormatterMediumStyle]; |
} |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
BirdSighting *sightingAtIndex = [self.dataController objectInListAtIndex:indexPath.row]; |
[[cell textLabel] setText:sightingAtIndex.name]; |
[[cell detailTextLabel] setText:[formatter stringFromDate:(NSDate *)sightingAtIndex.date]]; |
return cell; |
} |
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath |
{ |
// Return NO if you do not want the specified item to be editable. |
return NO; |
} |
- (IBAction)done:(UIStoryboardSegue *)segue |
{ |
if ([[segue identifier] isEqualToString:@"ReturnInput"]) { |
AddSightingViewController *addController = [segue sourceViewController]; |
if (addController.birdSighting) { |
[self.dataController addBirdSightingWithSighting:addController.birdSighting]; |
[[self tableView] reloadData]; |
} |
[self dismissViewControllerAnimated:YES completion:NULL]; |
} |
} |
- (IBAction)cancel:(UIStoryboardSegue *)segue |
{ |
if ([[segue identifier] isEqualToString:@"CancelInput"]) { |
[self dismissViewControllerAnimated:YES completion:NULL]; |
} |
} |
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { |
if ([[segue identifier] isEqualToString:@"ShowSightingDetails"]) { |
BirdsDetailViewController *detailViewController = [segue destinationViewController]; |
detailViewController.sighting = [self.dataController objectInListAtIndex:[self.tableView indexPathForSelectedRow].row]; |
} |
} |
@end |
Detail View Controller Files
This section contains listings for the following files:
BirdsDetailViewController.h
BirdsDetailViewController.m
BirdsDetailViewController.h
#import <UIKit/UIKit.h> |
@class BirdSighting; |
@interface BirdsDetailViewController : UITableViewController |
@property (strong, nonatomic) BirdSighting *sighting; |
@property (weak, nonatomic) IBOutlet UILabel *birdNameLabel; |
@property (weak, nonatomic) IBOutlet UILabel *locationLabel; |
@property (weak, nonatomic) IBOutlet UILabel *dateLabel; |
@end |
BirdsDetailViewController.m
#import "BirdsDetailViewController.h" |
#import "BirdSighting.h" |
@interface BirdsDetailViewController () |
- (void)configureView; |
@end |
@implementation BirdsDetailViewController |
#pragma mark - Managing the detail item |
- (void)setSighting:(BirdSighting *) newSighting |
{ |
if (_sighting != newSighting) { |
_sighting = newSighting; |
// Update the view. |
[self configureView]; |
} |
} |
- (void)configureView |
{ |
// Update the user interface for the detail item. |
BirdSighting *theSighting = self.sighting; |
static NSDateFormatter *formatter = nil; |
if (formatter == nil) { |
formatter = [[NSDateFormatter alloc] init]; |
[formatter setDateStyle:NSDateFormatterMediumStyle]; |
} |
if (theSighting) { |
self.birdNameLabel.text = theSighting.name; |
self.locationLabel.text = theSighting.location; |
self.dateLabel.text = [formatter stringFromDate:(NSDate *)theSighting.date]; |
} |
} |
- (void)viewDidLoad |
{ |
[super viewDidLoad]; |
// Do any additional setup after loading the view, typically from a nib. |
[self configureView]; |
} |
@end |
Add Scene View Controller Files
This section contains listings for the following files:
AddSightingViewController.h
AddSightingViewController.m
AddSightingViewController.h
#import <UIKit/UIKit.h> |
@class BirdSighting; |
@interface AddSightingViewController : UITableViewController <UITextFieldDelegate> |
@property (weak, nonatomic) IBOutlet UITextField *birdNameInput; |
@property (weak, nonatomic) IBOutlet UITextField *locationInput; |
@property (strong, nonatomic) BirdSighting *birdSighting; |
@end |
AddSightingViewController.m
#import "AddSightingViewController.h" |
#import "BirdSighting.h" |
@interface AddSightingViewController () |
@end |
@implementation AddSightingViewController |
- (BOOL)textFieldShouldReturn:(UITextField *)textField { |
if ((textField == self.birdNameInput) || (textField == self.locationInput)) { |
[textField resignFirstResponder]; |
} |
return YES; |
} |
- (void)viewDidLoad |
{ |
[super viewDidLoad]; |
// Uncomment the following line to preserve selection between presentations. |
// self.clearsSelectionOnViewWillAppear = NO; |
// Uncomment the following line to display an Edit button in the navigation bar for this view controller. |
// self.navigationItem.rightBarButtonItem = self.editButtonItem; |
} |
- (void)didReceiveMemoryWarning |
{ |
[super didReceiveMemoryWarning]; |
// Dispose of any resources that can be recreated. |
} |
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { |
if ([[segue identifier] isEqualToString:@"ReturnInput"]) { |
if ([self.birdNameInput.text length] || [self.locationInput.text length]) { |
BirdSighting *sighting; |
NSDate *today = [NSDate date]; |
sighting = [[BirdSighting alloc] initWithName:self.birdNameInput.text location:self.locationInput.text date:today]; |
self.birdSighting = sighting; |
} |
} |
} |
@end |
Copyright © 2013 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2013-10-22