I have used the UIPageViewController for loading around 5 images in a webview.
So whenever the below line of code
- (UIViewController *)pageViewController:(UIPageViewController *) pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
, is called, the return object is a class object(in my case class name is LoadPage). Everything works fine in my code, but I am facing some weird problem here and little uncomfortable.
So I planned to move to single class instead of two.
Now the return type for this method will be self.
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
return self;
}
Am I right?
But When I try to initialize the UIPageViewController, the application is getting crashed at one particular line.
- (void) createViewControllers {
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options: nil];
pageViewController.delegate = self;
pageViewController.dataSource = self;
NSArray* nextScreens = [NSArray arrayWithObject:self];
[pageViewController setViewControllers: nextScreens direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self addChildViewController:pageViewController];
[self.view addSubview:pageViewController.view];
[[self view] addSubview:[pageViewController view]]; //CRASH
[pageController didMoveToParentViewController:self];
CGRect pageViewRect = self.view.bounds;
pageViewRect = CGRectInset(pageViewRect,0, 0);
pageViewController.view.frame = pageViewRect;
self.view.gestureRecognizers = pageViewController.gestureRecognizers;
}
You cannot return self in
pageViewController:viewControllerAfterViewControllerbecause that would produce a cycle in the view controller hierarchy.You add the pageViewController as child view controller to self and then later the pageViewController adds self as child view controller (page) to itself. Thats not allowed!