Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 69c27a389f
Fetching contributors…

Cannot retrieve contributors at this time

86 lines (68 sloc) 3.296 kB
//
// BeautifulMapsModelController.m
// Beautiful Maps
//
// Created by Zach Williams on 4/29/12.
// Copyright (c) 2012 Zach Williams. All rights reserved.
//
#import "BeautifulMapsModelController.h"
#import "BeautifulMapsDataViewController.h"
/*
A controller object that manages a simple model -- a collection of month names.
The controller serves as the data source for the page view controller; it therefore implements pageViewController:viewControllerBeforeViewController: and pageViewController:viewControllerAfterViewController:.
It also implements a custom method, viewControllerAtIndex: which is useful in the implementation of the data source methods, and in the initial configuration of the application.
There is no need to actually create view controllers for each page in advance -- indeed doing so incurs unnecessary overhead. Given the data model, these methods create, configure, and return a new view controller on demand.
*/
@interface BeautifulMapsModelController()
@property (readonly, strong, nonatomic) NSArray *pageData;
@end
@implementation BeautifulMapsModelController
- (id)init
{
self = [super init];
if (self) {
// Create the data model.
_pageData = @[ @"SF", @"Seoul", @"Manhattan", @"Tokyo", @"DFW" ];
}
return self;
}
- (BeautifulMapsDataViewController *)viewControllerAtIndex:(NSUInteger)index storyboard:(UIStoryboard *)storyboard
{
// Return the data view controller for the given index.
if (([self.pageData count] == 0) || (index >= [self.pageData count])) {
return nil;
}
// Create a new view controller and pass suitable data.
BeautifulMapsDataViewController *dataViewController = [storyboard instantiateViewControllerWithIdentifier:@"BeautifulMapsDataViewController"];
dataViewController.dataObject = [self.pageData objectAtIndex:index];
return dataViewController;
}
- (NSUInteger)indexOfViewController:(BeautifulMapsDataViewController *)viewController
{
// Return the index of the given data view controller.
// For simplicity, this implementation uses a static array of model objects and the view controller stores the model object; you can therefore use the model object to identify the index.
return [self.pageData indexOfObject:viewController.dataObject];
}
#pragma mark - Page View Controller Data Source
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:(BeautifulMapsDataViewController *)viewController];
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return [self viewControllerAtIndex:index storyboard:viewController.storyboard];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:(BeautifulMapsDataViewController *)viewController];
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [self.pageData count]) {
return nil;
}
return [self viewControllerAtIndex:index storyboard:viewController.storyboard];
}
@end
Jump to Line
Something went wrong with that request. Please try again.