0

is there a way to programmatically tell when a certain page is showing in a pageviewcontroller? For example, I instantiated it as the following:

if (index == 0) {
        pageContentViewController = [storyboard instantiateViewControllerWithIdentifier:@"OnCampusTable"];
    }
    else if (index == 1) {
        pageContentViewController = [storyboard instantiateViewControllerWithIdentifier:@"OffCampusTable"];
    }
    else if (index == 2) {
        pageContentViewController = [storyboard instantiateViewControllerWithIdentifier:@"MyEventsTable"];
    }

I want to write something where

if (current page is "OnCampusTable") {
            method 1
        }
        else if (current page is "OffCampusTable") {
            method 2
        }
        else if (current page is "MyEventsTable") {
            method 3
        }

I've tried using the index, but because of the way that the pages load and stuff, it actually doesn't work properly. I was thinking of trying to access the page indicator thing (the little circle things) to get the page number, but I don't know how to do that.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
cwRichardKim
  • 1,030
  • 1
  • 9
  • 15

2 Answers2

0

You can use the delegate method pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted: to keep track of the current page and then execute some method depending on the page.

rocky
  • 3,521
  • 1
  • 23
  • 31
0

I've seen a few solutions out there that use an array of ViewControllers and do comparisons, but I don't want to keep a bunch of controllers around (I generate them on the fly from my model), so I found a solution that seems to work.

Basically I point a property to the prior and next controllers in the data source before and after methods. In the delegate's didFinishAnimating call I compare the new controller to these properties to see if we moved forward or backward. One caveat is that before and after don't get called if the controller has already been loaded, so I reassign my property based on didFinishAnimating's previousViewController's value.

Note, I only implemented this for the case where one page is displayed.

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    if (self.pageIndex > 0) {
        UIViewController *vc = [self genController:self.trackList[self.pageIndex - 1]];
        self.priorVC = vc;
        return vc;
    } else {
        self.priorVC = nil;
        return nil;
    }
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    if (self.pageIndex < self.trackList.count - 1) {
        UIViewController *vc = [self genController:self.trackList[self.pageIndex + 1]];
        self.nextVC = vc;
        return vc;
    } else {
        self.nextVC = nil;
        return nil;
    }
}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
    if (finished && completed) {
        if (pageViewController.viewControllers.lastObject == self.priorVC) {
            NSLog(@"Back");
            self.pageIndex--;
            self.nextVC = previousViewControllers.lastObject;
        } else if (pageViewController.viewControllers.lastObject == self.nextVC) {
            NSLog(@"Forward");
            self.pageIndex++;
            self.priorVC = previousViewControllers.lastObject;
        }
        NSLog(@"Page: %ld",self.pageIndex);
    }
}
EPage_Ed
  • 1,173
  • 11
  • 11