How do I add multiple ViewControllers to my UIPageViewController?

筅森魡賤 提交于 2019-12-04 15:00:31

Here is the fully working code if anyone is interested. Took me a while to get it right, but eventually this worked perfectly!

#import "PageViewController.h"

@interface PageViewController ()

@property (nonatomic, retain) UIViewController *first;
@property (nonatomic, retain) UIViewController *second;
@property (nonatomic, retain) UIViewController *third;
@property (nonatomic, retain) UIViewController *fourth;
@property (nonatomic, retain) UIViewController *fifth;

@end

@implementation PageViewController {
    NSArray *viewControllers;
}

- (UIViewController *)first {
    if (!_first) {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
        _first = [sb instantiateViewControllerWithIdentifier:@"1"];
    }
    return _first;
}

- (UIViewController *)second {
    if (!_second) {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
    _second = [sb instantiateViewControllerWithIdentifier:@"2"];
    }
    return _second;
}
- (UIViewController *)third {
    if (!_third) {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
    _third = [sb instantiateViewControllerWithIdentifier:@"3"];
    }
    return _third;
}
- (UIViewController *)fourth {
    if (!_fourth) {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
    _fourth = [sb instantiateViewControllerWithIdentifier:@"4"];
    }
    return _fourth;
}
- (UIViewController *)fifth {
    if (!_fifth) {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
    _fifth = [sb instantiateViewControllerWithIdentifier:@"5"];
    }
    return _fifth;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = self;

    // Aggancio il view controller iniziale.
    [self setViewControllers:@[self.first]
                   direction:UIPageViewControllerNavigationDirectionForward
                    animated:YES
                  completion:nil];
}

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

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

    UIViewController *nextViewController = nil;

    if (viewController == self.first) {
        nextViewController = self.second;
    }
    if (viewController == self.second) {
        nextViewController = self.third;
    }
    if (viewController == self.third) {
        nextViewController = self.fourth;
    }
    if (viewController == self.fourth) {
        nextViewController = self.fifth;
    }

    return nextViewController;
}

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

    UIViewController *prevViewController = nil;

    if (viewController == self.fifth) {
        prevViewController = self.fourth;
    }
    if (viewController == self.fourth) {
        prevViewController = self.third;
    }
    if (viewController == self.third) {
        prevViewController = self.second;
    }
    if (viewController == self.second) {
        prevViewController = self.first;
    }

    return prevViewController;
}

@end

This is old, but it helped me out a lot. For some reason your code had some problems for me, and I wasn't using storyboard. For anyone's future reference, this is the code I'm using that works:

#import "PageController.h"
#import "SeasonTixViewController.h"
#import "SeasonTicketPricesViewController.h"
#import "ArenaLayoutSeasonTixViewController.h"

@interface PageController ()

@property (nonatomic, retain) UIViewController *first;
@property (nonatomic, retain) UIViewController *second;
@property (nonatomic, retain) UIViewController *third;

@end

@implementation PageController {
NSArray *viewControllers;
}

- (UIViewController *)first {
if (!_first) {
    SeasonTixViewController *first = [[SeasonTixViewController alloc] init];
    _first = first;
}
return _first;
}

- (UIViewController *)second {
if (!_second) {
    SeasonTicketPricesViewController *second = [[SeasonTicketPricesViewController alloc] init];
    _second = second;
}
return _second;
}
- (UIViewController *)third {
if (!_third) {
    ArenaLayoutSeasonTixViewController *third = [[ArenaLayoutSeasonTixViewController alloc] init];
    _third = third;
}
return _third;
}


- (void)viewDidLoad {
[super viewDidLoad];

self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

[[self.pageController view] setFrame:self.view.bounds];

self.pageController.dataSource = self;

[self.pageController setViewControllers:@[self.first]
               direction:UIPageViewControllerNavigationDirectionForward
                animated:YES
              completion:nil];

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

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

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

UIViewController *nextViewController = nil;

if (viewController == self.first) {
    nextViewController = self.second;
}
if (viewController == self.second) {
    nextViewController = self.third;
}

return nextViewController;
}

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

UIViewController *prevViewController = nil;

if (viewController == self.third) {
    prevViewController = self.second;
}
if (viewController == self.second) {
    prevViewController = self.first;
}

return prevViewController;
}

- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
// The number of items reflected in the page indicator.
return 3;
}

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
// The selected item reflected in the page indicator.
return 0;
}

@end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!