2

I'm trying to add a series of UINavigationControllers to a single UIPageViewController as if adding swipe nav between pages. I did this with some success with standard view controllers, but I'm now trying to add nav controllers instead. Here's my Storyboard:

enter image description here

What I'd like to do is instantiate swipe nav (like Snapchat) between all pages in the 3rd row. Since they all lead to other view controllers, each is embedded within it's own nav controller. Here's my code:

class ViewController: UIViewController, UIPageViewControllerDataSource { // <-- Error here

// Sets up UIPageViewController. Must add to array count and instantiate new VCs.
    var myNavControllers = Array(count: 3, repeatedValue:UINavigationController())

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

        let pvc = segue.destinationViewController as! UIPageViewController

        pvc.dataSource = self // <-- Error here

        let storyboard = UIStoryboard(name: "Main", bundle: nil);

        var vc0 = storyboard.instantiateViewControllerWithIdentifier("Nav1") as! UINavigationController
        var vc1 = storyboard.instantiateViewControllerWithIdentifier("Nav2") as! UINavigationController
        var vc2 = storyboard.instantiateViewControllerWithIdentifier("Nav3") as! UINavigationController
        var vc3 = storyboard.instantiateViewControllerWithIdentifier("Nav4") as! UINavigationController


        self.myNavControllers = [vc0, vc1, vc2, vc3]

        pvc.setViewControllers([myNavControllers[1]], direction:.Forward, animated:true, completion:nil)

    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UINavigationController) -> UINavigationController? {
        var currentIndex =  find(self.myNavControllers, viewController)!+1
        if currentIndex >= self.myNavControllers.count {
            return nil
        }
        return self.myNavControllers[currentIndex]
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UINavigationController) -> UINavigationController? {
        var currentIndex =  find(self.myNavControllers, viewController)!-1
        if currentIndex < 0 {
            return nil
        }
        return self.myNavControllers[currentIndex]
    }

}

Problem is, I'm getting two errors. One at class: ViewController declaration: Type 'ViewController' does not conform to protocol 'UIPageViewControllerDataSource'. The other at pvc.dataSource = self, with error: Cannot assign a value of type 'ViewController' to a value of type 'UIPageViewControllerDataSource?'

Can what I'm trying to do be accomplished? If so, do I need to alter my code somewhere or achieve this an entirely different way?

slider
  • 2,736
  • 4
  • 33
  • 69
  • Got it thanks to this answer: http://stackoverflow.com/questions/25387436/using-uipageviewcontroller-with-swift-and-multiple-view-controllers – slider Jul 06 '15 at 23:45

1 Answers1

0

The protocol you have defined is in ViewController but that should be defined in UIPageViewController class.

class PageViewController: UIPageViewController, UIPageViewControllerDataSource {
}

For this, you need to create a new class which should be associated with Page View Controller that you have taken on storyboard.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97