0

I'm trying to display a button once the user swiped to the last viewcontroller (4/4). But the index incorrectly outputs the wrong number (even though correct view controller is being shown).

func viewControllerAtIndex(index : Int) -> UIViewController? {
    if((self.pageTitles.count == 0) || (index >= self.pageTitles.count)) {
        return nil
    } 
    let pageContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageContentViewController") as! PageContentViewController

    pageContentViewController.imageName = self.images[index]
    pageContentViewController.titleText = self.pageTitles[index]
    pageContentViewController.pageIndex = index


    println(pageContentViewController.imageName)
    if index  == 3 { // show start button once user reaches the last screen (index = 3)
        startBtn.hidden = false
    } else {
        startBtn.hidden = true
    }

    return pageContentViewController
}

All the views are shown correctly, except index randomly jumps from 1, 2, 4, 2. If I swipe back and forth it gets worse.

Why is the index not showing the correct index (consistent with the view controller being shown)?

entire code

Onichan
  • 4,476
  • 6
  • 31
  • 60
  • even `println(pageContentViewController.imageName)` is outputting an image that is not the same as the one being shown. – Onichan Jun 07 '15 at 21:48

1 Answers1

0

If your use of viewControllerAtIndex is anything like the project template provided by Xcode for "Page-Based Application", this is being called ultimately from one of the methods of UIPageViewControllerDataSource, i.e. pageViewController:viewControllerAfterViewController or pageViewController:viewControllerBeforeViewController. The time at which the page view controller calls these methods and the index provided are entirely up to its internal implementation and may have nothing to do with the order in which you are navigating and viewing pages. I would assume that it is highly optimized for memory consumption and performance, so it is asking for view controllers in a way that meets those goals.

Patrick Lynch
  • 2,742
  • 1
  • 16
  • 18
  • Thanks for the explanation. If that's the case, then would you mind suggesting how I can get the current index being displayed? – Onichan Jun 07 '15 at 22:01
  • I actually just tried that. But my method never gets called: `func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [AnyObject], transitionCompleted completed: Bool) { println("test call") }` – Onichan Jun 07 '15 at 22:19
  • It works for me. Are you certain that you set the `delegate` property on your page view controller to be whatever object is implementing that method? – Patrick Lynch Jun 07 '15 at 22:22
  • I'm not exactly sure. How should the delegate be set for the page view controller? – Onichan Jun 07 '15 at 22:26
  • If you're working from that same "Page-Based Application" template, look for where the data source is set: `self.pageViewController!.dataSource = self.modelController`. A delegate is just another object like that (it could even be the same object as your data source) that conforms to the `UIPageViewControllerDelegate` protocol and you assign it the same way: `self.pageViewController!.delegate = self.modelController`. – Patrick Lynch Jun 07 '15 at 22:29