0

How can I get the pending view controller value from this method?

-(void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers {
}

What is the code I can use to get the pending view controller value from this array? Any help would be greatly appreciated?

I have tried using this code:

 nextIndex = (int)[pendingViewControllers objectAtIndex:0];

But it keeps returning -1 instead of the next or previous value!

  • What do you mean the pending view controller value? `pendingViewControllers` is an array of view controller objects. It doesn't have anything to do with the actual index. – AdamPro13 May 13 '15 at 21:44

1 Answers1

2

The method passes in an array of the pending view controllers, not an index, so you can't cast it to an int. If you want to get the first view controller, you can do it like this,

UIViewController *vc = pendingViewController.firstObject;

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • I am trying to get the previous or next view controller from the current view controller? can I achieve that with that code? – Cool Kat Studios May 13 '15 at 22:01
  • @CoolKatStudios I've never used this method, but from my understanding of the documentation, I would say yes, this method will do that. – rdelmar May 13 '15 at 22:04
  • So the first object is the next or previous view controller ? – Cool Kat Studios May 13 '15 at 22:11
  • @CoolKatStudios It depends on which way you're swiping. If you swipe left , it's the next controller, if you swipe right, it's the previous one. I think the array will only contain one controller unless you're showing two pages side by side. – rdelmar May 13 '15 at 22:13
  • `-(void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers { UIViewController *vc = pendingViewControllers.firstObject; nextIndex = (int)vc; }` I used this code but It is returning huge numbers in the console? shouldn't it just return 1 or -1? etc ? – Cool Kat Studios May 13 '15 at 22:17
  • Figured it out, Thanks! `-(void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers { UIViewController *vc = pendingViewControllers.firstObject; NSUInteger currentIndex = [myViewControllers indexOfObject:vc]; nextIndex = (int)currentIndex; NSLog(@"Next Index = %d",nextIndex); }` – Cool Kat Studios May 13 '15 at 22:31