monkmonk
monkmonk

Reputation: 23

UIPageviewcontroller: starting in the middle

Unfortunately the search didn't help me out... I have the following problem: I have three UItableviews (ViewController1, ViewController2 and Viewcontroller3) which I embedded in a simple PageViewController Screenshot of storyboard ( I can't insert any pictures here yet :/).

In order to display the three ViewControllers I put them into an array in my viewdidload in my PageViewController.m (custom class for the PageViewController), so I didn't use a ViewController as a Container to lod the other ViewControllers into:

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    self.delegate = self;
    self.dataSource = self;
    
    UIViewController *p1 = [self.storyboard
                            instantiateViewControllerWithIdentifier:@"TableView1"];
    UIViewController *p2 = [self.storyboard
                            instantiateViewControllerWithIdentifier:@"TableView2"];
    UIViewController *p3 = [self.storyboard
                            instantiateViewControllerWithIdentifier:@"TableView3"];
    
    myViewControllers = @[p1,p2,p3];
    
I can pick which viewcontroller I start with and either

- go forwards(direction:UIPageViewControllerNavigation**DirectionForward**):

        [self setViewControllers:@[p1]
                       direction:UIPageViewControllerNavigationDirectionForward
                        animated:NO completion:nil];
        
        NSLog(@"loaded!");
    }

or pick the last on and go backwards (UIPageViewControllerNavigation**DirectionReverse**):

        [self setViewControllers:@[p3]
                   direction:UIPageViewControllerNavigationDirectionReverse
                   animated:NO completion:nil];
      

However, I've been trying to figure out how to start with ViewController2 and have Viewcontroller1 on its left and ViewController3 on the right (cp. Screenshot)

Any suggestions or code snippets would be hugely appreciated!

And here's the entire code:

#import "StartScreenPageViewController.h"

@interface StartScreenPageViewController ()

@end

@implementation StartScreenPageViewController
{
    NSArray *myViewControllers;
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    self.delegate = self;
    self.dataSource = self;
    
    UIViewController *p1 = [self.storyboard
                            instantiateViewControllerWithIdentifier:@"1"];
    UIViewController *p2 = [self.storyboard
                            instantiateViewControllerWithIdentifier:@"2"];
    UIViewController *p3 = [self.storyboard
                            instantiateViewControllerWithIdentifier:@"3"];
    
    myViewControllers = @[p1,p2,p3];
    
    [self setViewControllers:@[p1]
                   direction:UIPageViewControllerNavigationDirectionForward
                    animated:NO completion:nil];
    
    NSLog(@"loaded!");
}

-(UIViewController *)viewControllerAtIndex:(NSUInteger)index
{
    return myViewControllers[index];
}

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController
     viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSUInteger currentIndex = [myViewControllers indexOfObject:viewController];
    
    --currentIndex;
    currentIndex = currentIndex % (myViewControllers.count);
    return [myViewControllers objectAtIndex:currentIndex];
}

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController
      viewControllerAfterViewController:(UIViewController *)viewController
{
    NSUInteger currentIndex = [myViewControllers indexOfObject:viewController];
    
    ++currentIndex;
    currentIndex = currentIndex % (myViewControllers.count);
    return [myViewControllers objectAtIndex:currentIndex];
}

-(NSInteger)presentationCountForPageViewController:
(UIPageViewController *)pageViewController
{
    return myViewControllers.count;
}

-(NSInteger)presentationIndexForPageViewController:
(UIPageViewController *)pageViewController
{
    return 0;
}

@end

Upvotes: 1

Views: 392

Answers (1)

Daniel J
Daniel J

Reputation: 456

You should be able to set the initial view controller in viewDidLoad. You're already doing it, but you're setting it to the first view controller:

 [self setViewControllers:@[p1]
                   direction:UIPageViewControllerNavigationDirectionForward
                    animated:NO completion:nil];

I think you should just be able to change this to:

 [self setViewControllers:@[p2]
                   direction:UIPageViewControllerNavigationDirectionForward
                    animated:NO completion:nil];

Then just make sure your pageViewController:viewControllerAfterViewController and pageViewController:viewControllerBeforeViewController methods handle iterating up and down the data source per your desired functionality.

Upvotes: 1

Related Questions