0

I´m trying to create a view that would move to some ViewController based on another element that returns an index of the object that it is showing.

I´m able to show the specific ViewController once, but when I return back from it, I will always get the same ViewControllerone no matter which index is on screen. app design

Here is my code:

In ViewDidLoad I setup the PageViewControllerand add the iCarousel as a subview to it.

self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationVertical options:nil];

self.pageViewController.dataSource = self;
[[self.pageViewController view] setFrame:[[self view] bounds]];

_controller1 = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
_controller2 = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstViewController"];
_controller3 = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];

self.viewControllers = [NSArray arrayWithObjects:_controller1, nil];

[self.pageViewController setViewControllers:self.viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
[self.view addSubview:_carousel];

And here is the code that controls which ViewControllershould show on screen when user pulls the view up.

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    if (self.pageViewController.viewControllers[0] == self.controller2)
        return self.controller1;
    return nil;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    if (self.pageViewController.viewControllers[0] == self.controller1) {
        if ([self.carousel currentItemIndex] == 2) {
            NSLog(@"Giving view %d", [self.carousel currentItemIndex]);

            return self.controller2;

        } else if ([self.carousel currentItemIndex] == 3) {
            NSLog(@"Giving view %d", [self.carousel currentItemIndex]);

            return self.controller3;
        }

    }
    return nil;
}

Do I have to destroy the pulled ViewController somehow, or is this even possible with the UIPageViewController?

Thanks.

EDIT: Tried to instantiate the ViewControllers when I would transition to them:

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {

    if ([self.carousel currentItemIndex] == 2) {
        NSLog(@"load first vc %d", [self.carousel currentItemIndex]);
        FirstViewController *secondVC = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstViewController"];
        return secondVC;
    } else if ([self.carousel currentItemIndex] == 3) {
        NSLog(@"load third vc 2 %d", [self.carousel currentItemIndex]);
        SecondViewController *thirdVC = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
        return thirdVC;
    }
    return nil;
}

With this method I'm seeing the log messages like it was working right but the right View is still loaded only on first time. Also first time I transition to SecondViewController or ThirdViewControllerthe I get the log message twice.

Edit: Just noticed that with this I'm able to keep scrolling down infinitely as it keeps loading the ViewController again and again. So if I scroll the iCarousel to index 2 and pull up for its View I get first the previous view but if "pull" again I get the right one.

I feel that I'm very close but what is the little thing that I'm missing?

FINAL EDIT:

Ok apparently my code was correct since the beginning. Here are some and some more discussions about a bug in UIPageViewControllerTransitionStyleScroll. So simply changing transition to curl did what I was trying to do.

If someone could answer how and where I would implement thisthe suggested answer that would great.

Community
  • 1
  • 1
Pahnev
  • 716
  • 5
  • 26

1 Answers1

0

The method one uses to change the UIPageView to a particular is:

- (void)setViewControllers:(NSArray *)viewControllers direction:(UIPageViewControllerNavigationDirection)direction animated:(BOOL)animated completion:(void (^)(BOOL finished))completion;

To set a particular viewController, you should create a method that encapsulates the method and gets a viewController at an index. To do this you should place your view controllers in an array, so you could reference them with this method.

-(void)setView:(NSUInteger)index {    
    // create a view controller
    UIViewController *startingViewController = (UIViewController *)[self.arrayOfViewControllers objectAtIndex:index];
    // place it in an array
    NSArray *viewControllers = @[startingViewController];
    // Above method
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];
}
  • I am a bit lost. How exactly would I use this? Do I use that method inside `UIPageViewControllerDataSource` methods or where? – Pahnev Nov 24 '14 at 15:25
  • You can use the setViewControllers wherever you need it. –  Nov 25 '14 at 03:40