如何将多个ViewControllers添加到我的UIPageViewController?

时间:2014-11-23 07:50:05

标签: objective-c uiviewcontroller uipageviewcontroller

所以我对Obj-C很新,并尝试查看示例代码和其他在线资源来回答我的问题,但我似乎无法找到真正有用的东西。基本上我要做的就是将我在Storyboard中创建的多个UIViewControllers添加到UIPageViewController中 - 我寻求帮助的第一个地方是使用PageBased应用程序模板的Xcode本身。这实际上帮助了很多,我得到了一个PageController启动和运行。然后我转向在线资源,但大多数(如果不是全部)使用单个viewController(如this)。然后我转向StackOverflow并找到以下内容 - How to implement UIPageViewController that utilizes multiple ViewControllers。 以上资源让我走得很远: 在PageViewController.h中:

    #import <UIKit/UIKit.h>

@interface PageViewController : UIPageViewController <UIPageViewControllerDataSource>

@end

在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

所有这些代码都加载了第一个现在可以刷卡的视图控制器,但实际上并没有向任何东西滑动 - 为什么会这样?我究竟做错了什么?上述资源使用标题和页面来创建视图,我真的不知道如何去做。有人会介意指导我或在正确的方向上推动我吗?任何帮助将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:6)

如果有兴趣的话,这是完整的代码。花了一些时间才弄清楚,但最终这完美无缺!

#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

答案 1 :(得分:3)

这是旧的,但它帮助了我很多。出于某种原因,您的代码对我来说有些问题,而且我没有使用故事板。对于任何人未来的参考,这是我正在使用的代码:

#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
相关问题