title: Implementing UIPageViewController's logic as a subview to a UIViewController in Swift
body: <p>I have to implement a UIPageViewController as a subview to my UIViewController. The UIPageViewController has some logic and other ViewControllers inside. My question is how to connect these two pieces of code below.</p>
  
  <p>So this is my main UIViewController code:</p>
  
  <pre><code>class MyMainViewController: UIViewController {
  
      override func viewDidLoad() {
         super.viewDidLoad()
      }
      override func didReceiveMemoryWarning() {
         super.didReceiveMemoryWarning()
      }   
  }
  </code></pre>
  
  <p>and my UIPageViewController code, which implements an endless loop between ViewControllers that he contains (I copied the whole code, probably it wasn't necessary ): </p>
  
  <pre><code>class TutorialPageViewController: UIPageViewController {
  
      private(set) lazy var orderedViewControllers: [UIViewController] = {
          return [self.newColoredViewController("Green"),
                  self.newColoredViewController("Red"),
                  self.newColoredViewController("Blue")]
      }()
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          dataSource = self
  
          if let firstViewController = orderedViewControllers.first {
              setViewControllers([firstViewController],
                  direction: .Forward,
                  animated: true,
                  completion: nil)
          }
      }
  
      private func newColoredViewController(color: String) -&gt; UIViewController {
          return UIStoryboard(name: "Main", bundle: nil) .
          instantiateViewControllerWithIdentifier("\(color)ViewController")
      }
  
  }
  
  extension TutorialPageViewController: UIPageViewControllerDataSource {
  
      func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -&gt; UIViewController? {
          guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
              return nil
          }
  
          let previousIndex = viewControllerIndex - 1
          guard previousIndex &gt;= 0 else {
              return orderedViewControllers.last
          }
          guard orderedViewControllers.count &gt; previousIndex else {
              return nil
          }
          return orderedViewControllers[previousIndex]
      }
  
      func pageViewController(pageViewController: UIPageViewController,
      viewControllerAfterViewController viewController: UIViewController) -&gt; UIViewController? {
  
          guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
              return nil
          }
  
          let nextIndex = viewControllerIndex + 1
          let orderedViewControllersCount = orderedViewControllers.count
  
          guard orderedViewControllersCount != nextIndex else {
              return orderedViewControllers.first
          }
  
          guard orderedViewControllersCount &gt; nextIndex else {
              return nil
          }
  
          return orderedViewControllers[nextIndex]
      }
  
      func presentationCountForPageViewController(pageViewController: UIPageViewController) -&gt; Int {
          return orderedViewControllers.count
      }
  
      func presentationIndexForPageViewController(pageViewController: UIPageViewController) -&gt; Int {
          guard let firstViewController = viewControllers?.first,
          firstViewControllerIndex = orderedViewControllers.indexOf(firstViewController) else {
              return 0
          }
  
          return firstViewControllerIndex
      }
  
  }
  </code></pre>