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 ViewController
one no matter which index is on screen.
Here is my code:
In ViewDidLoad
I setup the PageViewController
and 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 ViewController
should 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 ThirdViewController
the 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.