Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIPageViewController with different ViewControllers, right way?

Basically, I have a Table View Controller and a normal View Controller, I want to make it so that I can swipe between the view controllers like on the home screen of app (obvious same type of view controller).

But the thing I am finding is that the UIpageviewcontroller is usaully used when you have multiple views of the same type of viewcontroller for example swiping between pictures, you could just set an index in the class and an array of images relating to the index and set it up that way.

But I'm trying to get different view controllers in this set up. Right now it's two but will definitely go up to about 4.

Is UIPageViewController the right way to set it up or something else.

EDIT: Code added below

    //
//  ViewController.m
//  testb
//
#import "MainPostController.h"
#import "MainTableViewController.h"
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize pageViewController;
@synthesize indexValue = _indexValue;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.indexValue= 1;
    NSLog(@"main intitated");

    // Do any additional setup after loading the view, typically from a nib.
    self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
    self.pageViewController.dataSource = self;

    MainTableViewController *tvc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainTableViewController"];
    MainPostController *pvc;
    NSArray *viewcontrollers =[NSArray arrayWithObjects:tvc, pvc, nil];

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

    self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 30);

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    if (self.indexValue == 1) {
        MainPostController *pvc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainPostController"];
        self.indexValue = 0 ;
        return  pvc;

    }
    else return nil;




}

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

    if (self.indexValue == 0) {
        MainTableViewController *tvc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainTableViewController"];
        self.indexValue = 1;
        return tvc;

    }
    else return nil;


}
@end

I want the intial view to be the table view controller(tvc) and i want the Post view controller(pvc) to be to its left. I can load it up and swipe between the two easily but sometime i can swipe further than the boundaries so basically if i start on the tvc-->pvc-->tvc then i can go further by swiping right to the same tvc then the boundary is reached or go the other way tvc-->pvc-->tvc-->pvc-->pvc by swiping left for the last transition. The boundaries are reached in the end. Once the app intially launches at tvc i cannot go forward a page, (which is what should happen) and if i go tvc-->pvc and try to go to a page to the left it wont let me (which is what should happen)

Thanks

like image 505
lbpman1 Avatar asked Dec 22 '13 22:12

lbpman1


1 Answers

Finally done it, didn't have to add any properties to the different classes. Just tested the class. Heres the code:

-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    if ([viewController isKindOfClass:[MainTableViewController class]]) {
        MainPostController *pvc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainPostController"];
        return  pvc;

    }

    else return nil;

}


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

    if ([viewController isKindOfClass:[MainPostController class]]) {
        MainTableViewController *tvc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainTableViewController"];
        return tvc;

    }
    else return nil;
}

Should be easy to add more view controllers as i just need to add else if statements to test them. The main problem solver was the method isKindOfClass:

Thanks for your reply guys!

like image 133
lbpman1 Avatar answered Sep 30 '22 11:09

lbpman1