0

These are my view controllers: enter image description here

I want to make it so that I can swipe between the three of them starting in the middle one. All of the tutorials I've found require you to start from scratch and don't show me how to connect the three I have. Does anyone have a step-by-step instruction of how to do this?

Any help would be greatly appreciated

evanhaus
  • 727
  • 3
  • 12
  • 30

3 Answers3

2

First Create a class called PageViewController, drag a UIPageViewController in storyboard.For now lets set it as initial view controller from attributes inspector. Also from identity inspector set PageViewController as Class.

Call your three view controller for example StepZero,StepOne,StepTwo Also give them identifier in storyboard.

lets deep into coding now, so in PageViewController should subclass UIPageVIewController:

import UIKit

class PageViewController : UIPageViewController,UIPageViewControllerDataSource {

    var selectedIndex = 1

    override func viewDidLoad() {

        dataSource = self

        view.backgroundColor = UIColor.darkGrayColor()

        // This is the starting point.  Start with step zero.
        setViewControllers([getStepOne()], direction: .Forward, animated: false, completion: nil)
    }

    func getStepZero() -> StepZero {
        return storyboard!.instantiateViewControllerWithIdentifier("StepZero") as! StepZero
    }

    func getStepOne() -> StepOne {
        return storyboard!.instantiateViewControllerWithIdentifier("StepOne") as! StepOne
    }

    func getStepTwo() -> StepTwo {
        return storyboard!.instantiateViewControllerWithIdentifier("StepTwo") as! StepTwo
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
        if viewController.isKindOfClass(StepTwo) {
            // 2 -> 1
            return getStepOne()
        } else if viewController.isKindOfClass(StepOne) {
            // 1 -> 0
            return getStepZero()
        } else {
            // 0 -> end of the road
            return nil
        }
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        if viewController.isKindOfClass(StepZero) {
            // 0 -> 1
            return getStepOne()
        } else if viewController.isKindOfClass(StepOne) {
            // 1 -> 2
            return getStepTwo()
        } else {
            // 2 -> end of the road
            return nil
        }
    }

    // Enables pagination dots
    func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
        return 3
    }

    // This only gets called once, when setViewControllers is called
    func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
        return selectedIndex
    }


}

Let's say in Storyboard you have three viewControllers, you should set identifier for them from identity inspector as StepZero StepOne StepTwo for example and when you instantiate them you do :

func getStepZero() -> StepZero {
    return storyboard!.instantiateViewControllerWithIdentifier("StepZero") as! StepZero
}

func getStepOne() -> StepOne {
    return storyboard!.instantiateViewControllerWithIdentifier("StepOne") as! StepOne
}

func getStepTwo() -> StepTwo {
    return storyboard!.instantiateViewControllerWithIdentifier("StepTwo") as! StepTwo
}

The selected index is the index you want to start with which is number 1. And to start with second view controller call getStepOne() in setViewControllers. if you want to start with view controller 3 use selected index 2 and call getStepTwo()...etc

Download Updated Sample : https://mega.nz/#!EQEFhbwS!0yoy5RvAliQNnjRevWo05wPWk7P08e8DVetRZdjg-ro

AaoIi
  • 8,288
  • 6
  • 45
  • 87
  • I'm getting an error: "'Use of undeclared type ViewController'" any solutions for this? – evanhaus Jul 13 '16 at 04:20
  • @evanhaus ViewController is the name of your class. In my sample it's called ViewControlller , maybe in your sample it's called something else ! – AaoIi Jul 13 '16 at 04:24
  • Okay cool! One more issue, "MyViewController has no member "pageIndex" – evanhaus Jul 13 '16 at 04:39
  • @evanhaus You can add in your MyViewControlller var pageIndex : Int? Should make the error goes away . – AaoIi Jul 13 '16 at 04:50
  • @Aaoli I tried to load the following after a person signs in: `let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let homeViewController: UIViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("PageViewController") self.presentViewController(homeViewController, animated: true, completion: nil)` – evanhaus Jul 13 '16 at 05:30
  • @Aaoli but I get this error from this line of code: "self.setViewControllers([pageContentViewController!], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)" is giving me the following error: "warning: could not load any Objective-C class information. This will significantly reduce the quality of type information available." Any idea what that means? – evanhaus Jul 13 '16 at 05:31
  • @evanhaus , please download the updated project. i have updated my answer. – AaoIi Jul 14 '16 at 19:18
0

You can connect your view controllers by navigation controller . Just select one of your view controllers -> Editor(on the top bar of mac) -> Embed in -> Navigation controller.

Also if you want to swipe you can use a scroll view and only on view controller . Scroll view with content size of 3 view controllers can help you do the same. Thank you

0

Follow as below image

enter image description here

Give storyboard identifier as shown in below image

enter image description here

On button click push new viewcontroller

var viewControllerObj=self.storyboard!.instantiateViewControllerWithIdentifier("your storyboard identifier") 
self.navigationController!.pushViewController(viewControllerObj, animated: true)
Amit Jagesha シ
  • 1,092
  • 12
  • 21