Bummer! You must be logged in to access this page.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Page View Controller question

Using the page view template in Xcode 6.1 (swift), I've been messing around with different page views. The way it has it set up is that it creates each page view according to an array (as opposed to creating a 3 different view controllers). My question is, where in the code can I actually manipulate each page?

they start you off with flipping 12 pages according to the calendar. I got it to just do the 3 that I want, but I'm not sure how to change things for each view. here is my code below for the ModelController:

import UIKit

/*
 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.
 */


class ModelController: NSObject, UIPageViewControllerDataSource {

    var pageData = NSArray()


    override init() {
        super.init()
        // Create the data model.
        let dateFormatter = NSDateFormatter()
        pageData = ["Verse","Message","Application"]
    }

    func viewControllerAtIndex(index: Int, storyboard: UIStoryboard) -> DataViewController? {
        // 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.
        let dataViewController = storyboard.instantiateViewControllerWithIdentifier("DataViewController") as DataViewController
        dataViewController.dataObject = self.pageData[index]

        return dataViewController

    }

    func indexOfViewController(viewController: DataViewController) -> Int {
        // 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.
        if let dataObject: AnyObject = viewController.dataObject {
            return self.pageData.indexOfObject(dataObject)
        } else {
            return NSNotFound
        }

    }

    // MARK: - Page View Controller Data Source

    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
        var index = self.indexOfViewController(viewController as DataViewController)
        if (index == 0) || (index == NSNotFound) {
            return nil
        }

        index--
        return self.viewControllerAtIndex(index, storyboard: viewController.storyboard!)
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        var index = self.indexOfViewController(viewController as DataViewController)
        if index == NSNotFound {
            return nil
        }

        index++
        if index == self.pageData.count {
            return nil
        }
        return self.viewControllerAtIndex(index, storyboard: viewController.storyboard!)
    }

}

Can someone point me in the right direction? thanks!!

2 Answers

The answer to this will depend on what you want to achieve.

Page View Controllers are made up of 3 things: RootView, Model, DataView. You probably won't need to fiddle around with the RootView (depends on what you're doing) so I'll leave it out for now.

Model: Here is where you define the content you are going to show (e.g. image1)

Data: Here is where you define the type of content you are going to show (e.g. you are going to want to have a UIButton and a UIImage on the view)

To achieve a page view where each screen is different you will have to ensure your Model and DataView are designed to fit your purpose. For example, say you want to create a Page View with 3 screens but with these specifications:

  1. Screen 1 will only have 1 image
  2. Screen 2 will have 2 images and 1 button
  3. Screen 3 will have 3 images but you will be able to interact with them

You do all the type definitions in the DataView and declare a bunch of public properties related to them. In the Model in the indexOfViewController function, you declare the actual content. It will probably help to create some sort of index property in the DataView so that the page setup will be dependent on which screen you are showing (i.e. if self.pageIndex = 1, then just define 1 UIImage. If self.pageIndex=2, then define 2 UIImages and 1 UIButton).

THANK YOU Nick Sint !! Your explanation is exactly what I needed!