so I'm pretty new to Obj-C and have tried looking at sample code and other online resources to answer my question, but I can't seem to find anything that really helps. Essentially what I'm trying to do is to add multiple UIViewControllers that I created in Storyboard to a UIPageViewController - the first place I looked to for help was Xcode itself by using a template of a PageBased application. That actually helped quite a lot and I got a PageController up and running. Then I turned to online resources but most (if not all of them) use a single viewController (like this). Then I turned to StackOverflow and found the following - How to implement UIPageViewController that utilizes multiple ViewControllers. The above resources got me this far: In PageViewController.h:
#import <UIKit/UIKit.h>
@interface PageViewController : UIPageViewController <UIPageViewControllerDataSource>
@end
In PageViewController.m:
#import "PageViewController.h"
@interface PageViewController ()
@end
@implementation PageViewController {
NSArray *viewControllers;
UIViewController *first;
UIViewController *second;
UIViewController *third;
UIViewController *fourth;
UIViewController *fifth;
}
- (UIViewController *)first {
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
first = [sb instantiateViewControllerWithIdentifier:@"1"];
return first;
}
- (UIViewController *)second {
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
second = [sb instantiateViewControllerWithIdentifier:@"2"];
return second;
}
- (UIViewController *)third {
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
third = [sb instantiateViewControllerWithIdentifier:@"3"];
return third;
}
- (UIViewController *)fourth {
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Secondary" bundle:nil];
fourth = [sb instantiateViewControllerWithIdentifier:@"4"];
return fourth;
}
- (UIViewController *)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 == 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;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
All this code does is load the first view controller which is now swipe-able, but doesn't actually swipe towards anything - why is this?. What am I doing wrong? The aforementioned resources used titles and pages to create views and I really don't know how to go about this. Would anyone mind guiding me or nudging me in the correct direction? Any help would be greatly appreciated. Thanks!
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
来源:https://stackoverflow.com/questions/27086747/how-do-i-add-multiple-viewcontrollers-to-my-uipageviewcontroller