title: Detect current page of UIPageViewController
body: <p>I want to detect the current page in order to modify a button's title while the last page is showing and re-modify every time the user swipes back from the last to the second to last page. </p>
  
  <p>Now these are my delegate methods and my array of ViewControllers:</p>
  
  <pre><code>lazy var VCArr: [UIViewController] = {
      let firstVC = self.storyboard!.instantiateViewController(withIdentifier: "FirstTutorialPage")
      let secondVC = self.storyboard!.instantiateViewController(withIdentifier: "SecondTutorialPage")
      let thirdVC = self.storyboard!.instantiateViewController(withIdentifier: "ThirdTutorialPage")
      let fourthVC = self.storyboard!.instantiateViewController(withIdentifier: "FourthTutorialPage")
      return [firstVC, secondVC, thirdVC, fourthVC]
  }()
  </code></pre>
  
  <p>delegate methods</p>
  
  <pre><code>    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -&gt; UIViewController? {
  
      guard let viewControllerIndex = self.VCArr.index(of: viewController) else {
          return nil
      }
  
      let previousIndex = viewControllerIndex - 1
  
      guard previousIndex &gt;= 0 else {
          return nil
      }
  
      guard self.VCArr.count &gt; previousIndex else {
          return nil
      }
  
      return self.VCArr[previousIndex]
  }
  
  func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -&gt; UIViewController? {
      guard let viewControllerIndex = self.VCArr.index(of: viewController) else {
          return nil
      }
  
      let nextIndex = viewControllerIndex + 1
  
      guard nextIndex &lt; self.VCArr.count else {
          return nil
      }
  
      guard self.VCArr.count &gt; nextIndex else {
          return nil
      }
  
      return self.VCArr[nextIndex]
  }
  </code></pre>
  
  <p>Can someone help me detect with accuracy the last page shown? I tried some methods but everyone of them showed me the wrong index. Those methods detected the last index while showing the second to last page.</p>
  
  <p>Thanks to all.</p>