具有多个ViewControllers的UIPageViewController

时间:2014-08-01 20:24:56

标签: ios objective-c uipageviewcontroller

基本上我希望我的应用的目标网页具有与我的网页浏览控制器的网页内容视图不同的布局。我无法将起始视图控制器设置为我的目标网页。

这是我初始化pageViewController

的代码
// Creating the onboarding data
_pageCaptions = @[@"",@"How we do it",@"What we plan to do"];
_pageImages = @[@"Landing.png", @"OnboardingHolder1.png", @"OnboardingHolder2.png"];

// Create page view controller
self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
self.pageViewController.dataSource = self;

GOVPageContentViewController *startingViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = @[startingViewController];
NSLog(@"%@", viewControllers[0]);


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

// Change the size of page view controller
self.pageViewController.view.frame = CGRectMake(0, 75, self.view.frame.size.width, self.view.frame.size.height - 100);

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

以下是处理视图控制器之间导航的代码:

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

if ([viewController isKindOfClass:[LandingContentViewController class]]) {
    return nil;
} else {

    NSUInteger index = ((GOVPageContentViewController*) viewController).pageIndex;

    if (index == 0) {
        LandingContentViewController *landing = [self.storyboard instantiateViewControllerWithIdentifier:@"LandingContentViewController"];
        return landing;
    }

    if (index == NSNotFound) {
        return nil;
    }

    index--;
    return [self viewControllerAtIndex:index];
}}

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


if ([viewController isKindOfClass:[LandingContentViewController class]]) {
    return [self viewControllerAtIndex:0];

} else {
    NSUInteger index = ((GOVPageContentViewController*) viewController).pageIndex;

    if (index == NSNotFound) {
        return nil;
    }

    index++;
    if (index == [self.pageCaptions count]) {
        return nil;
    }
    return [self viewControllerAtIndex:index];
}}

- (GOVPageContentViewController *)viewControllerAtIndex:(NSUInteger)index {
if (([self.pageCaptions count] == 0) || (index >= [self.pageCaptions count])) {
    return nil;
}

// Create a new view controller and pass suitable data.
GOVPageContentViewController *pageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageContentViewController"];
pageContentViewController.imageFile = self.pageImages[index];
pageContentViewController.captionText = self.pageCaptions[index];
pageContentViewController.pageIndex = index;

return pageContentViewController;}

正如代码所示,我在模拟器中呈现的第一个视图是" GOVPageContentViewControllers中的第一个。"我可以在此页面视图上向右滑动,以便正确显示目标网页。这是可以理解的,因为我将startingViewController设置为GOVPageContentViewController的第一个。

如何正确添加LandingContentViewController作为我用于setViewControllers的第一个viewController?

2 个答案:

答案 0 :(得分:0)

您应该呈现viewController而不是view。 使用以下代码:

[self.navigationController presentViewController:_pageViewController animated:NO completion:nil];

而不是

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

答案 1 :(得分:0)

您似乎有两种选择:

  1. 展示您的#34;登陆"查看控制器,然后让它(通过按钮或计时器或其他)呈现你的PageViewController。

  2. 展示你的PageViewController,并设置你的#34;登陆"的实例。页面作为第一页面内容视图控制器。 (没有理由你不能这样做。你的页面内容视图控制器不必都是同一类型。)

  3. 使用#1,一旦呈现PageViewController,它只包含页面内容项。

    使用#2,您的PageViewController将着陆页和其他页面都显示为内容项。如果您不希望在用户滚动之后出现着陆页,只需调整VC-before / VC-after函数,以便在滚动"返回"时省略它。