title: iOS Swift - Change UIPageViewController page using UISegmentedControl
body: <p>I finnaly synchronized my segmentedController to the UIPageViewController in this way: when I swipe between pages, the segmented controller changes it's segment index. I want to know how to do the reverse too, when segmentedController's segment is tapped, to change the pageViewController page by segmentedController index. Here is my code:</p>
  
  <pre><code>class photoPageViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate, UINavigationControllerDelegate {
  
  
  @IBOutlet var segmentedControl: UISegmentedControl!
  private var pageViewController: UIPageViewController?
  var controllers = [thePageViewController]()
  var thePage = thePageViewController()
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      createPageViewController()
      setupPageControl()
  }
  
  private func createPageViewController() {
  
      var pageController = self.storyboard!.instantiateViewControllerWithIdentifier("PageController") as! UIPageViewController
      pageController.dataSource = self
      pageController.delegate = self
  
      var firstController = getItemController(thePage.itemIndex)!
      var startingViewControllers: NSArray = [firstController]
          pageController.setViewControllers(startingViewControllers as [AnyObject], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
      pageViewController = pageController
      addChildViewController(pageViewController!)
      self.view.addSubview(pageViewController!.view)
      pageViewController!.didMoveToParentViewController(self)
  }
  
  private func setupPageControl() {
      let appearance = UIPageControl.appearance()
  
      appearance.pageIndicatorTintColor = UIColor.grayColor()
      appearance.currentPageIndicatorTintColor = UIColor.whiteColor()
      appearance.backgroundColor = UIColor.darkGrayColor()
  }
  
  func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -&gt; UIViewController? {
  
      let itemController = viewController as! thePageViewController
  
      if itemController.itemIndex &gt; 0 {
          return getItemController(itemController.itemIndex-1)
      }
  
      return nil
  }
  
  func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -&gt; UIViewController? {
  
      let itemController = viewController as! thePageViewController
  
      if itemController.itemIndex+1 &lt; 3 {
          return getItemController(itemController.itemIndex+1)
      }
  
      return nil
  }
  
  func getItemController(itemIndex: Int) -&gt; UIViewController? {
      var vc: thePageViewController? = nil
      switch itemIndex {
      case 0:
          vc = self.storyboard!.instantiateViewControllerWithIdentifier("ViewController0") as! firstPhotoViewController
          vc?.itemIndex = itemIndex
  
  
      case 1:
          vc = self.storyboard!.instantiateViewControllerWithIdentifier("ViewController1") as! secondPhotoViewController
          vc?.itemIndex = itemIndex
  
  
      case 2:
          vc = self.storyboard!.instantiateViewControllerWithIdentifier("ViewController2") as! thirdPhotoViewController
          vc?.itemIndex = itemIndex
  
      default:
          return nil
  
      }
      return vc
  }
  
  func presentationCountForPageViewController(pageViewController: UIPageViewController) -&gt; Int {
      return 3
  }
  
  func presentationIndexForPageViewController(pageViewController: UIPageViewController) -&gt; Int {
      return 0
  }
  
  func pageViewController(photoPageViewController: UIPageViewController,
      didFinishAnimating finished: Bool,
      previousViewControllers pageViewController: [AnyObject],
      transitionCompleted completed: Bool)
  {
      if (!completed)
      {
  
          return;
      }
  
      segmentedControl.selectedSegmentIndex = thePageIndex 
  //thePageIndex is a global variable that changes when views from pageViewController appear
  
  }
  
  @IBAction func segmentedFunction(sender: AnyObject) {
  
      switch segmentedControl.selectedSegmentIndex
      {
  
      case 0:
          getItemController(0)
  
  
      case 1:
          getItemController(1)
  
      case 2:
          getItemController(2)
  
      default:
          println("...")
      }
  
  }}   
  </code></pre>