如何在iOS 5中向UIPageViewController添加一个页面数组? [英] How to add an Array of pages to a UIPageViewController in iOS 5?

查看:91
本文介绍了如何在iOS 5中向UIPageViewController添加一个页面数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了将我自己的视图控制器手动添加到UIPageViewController方法,有没有人知道我缺少什么?

Does anyone know what am I missing in order to add manually my own view controllers to the UIPageViewController methods?

我目前有这个,我不知道如何继续:

I currently have this and I do not know how to proceed:

NSDictionary *pageViewOptions = [NSDictionary dictionaryWithObjectsAndKeys:UIPageViewControllerOptionSpineLocationKey, [NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin], nil];

self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:pageViewOptions];


BookViewController *pageOne = [self.storyboard instantiateViewControllerWithIdentifier:@"BookViewController"];

AnotherPage *pageTwo = [self.storyboard instantiateViewControllerWithIdentifier:@"AnotherPage"];

PageThree *pageThree = [self.storyboard instantiateViewControllerWithIdentifier:@"PageThree"];


self.pageViewController.delegate = self;
self.pageViewController.dataSource = self;

[self.pageViewController setViewControllers:[NSArray arrayWithObjects:pageOne, pageTwo, pageThree, nil] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

[self addChildViewController:self.pageViewController];

[self.view addSubview:self.pageViewController.view];

self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;

[self.pageViewController didMoveToParentViewController:self];

但它似乎不起作用。我只看到RootViewController。

But it does not seem to work. I only see the RootViewController.

感谢所有喜欢提前帮助像我这样渴望新手的人.... :)

推荐答案

你设置它的方式并不完全是 UIPageViewController 的工作方式。

The way you've set this up isn't quite how UIPageViewController works.

首先要理解的是 setViewControllers:direction:animated:completion 只设置可见查看控制器,而不是您可能希望向用户显示的所有视图控制器。 (也就是说,如果我尝试在代码中设置三个控制器的数组,我会得到一个例外。)

The first thing to understand is that setViewControllers:direction:animated:completion only sets the visible view controllers, not all the view controllers you may wish to display to the user. (To wit, if I try to set an array of three controllers as in your code, I get an exception.)

其次,如果你有一个以上的可见页面(或者两个,取决于脊椎位置),必须实现 UIPageViewControllerDataSource 协议,以提供页面视图控制器将显示的视图控制器。

Secondly, if you have more than one visible page (or two, depending on spine location), you must implement the UIPageViewControllerDataSource protocol to provide the view controllers the page view controller will display.

这是一个简短的例子(作为 UIPageViewController 的子类实现,但也适用于子页面视图控制器。 ..只需用 myPageViewController.dataSource = ... 替换 self.dataSource = ...

Here's a short example (implemented as a subclass of UIPageViewController, but would work with a child page view controller, too... just replace self.dataSource = ... with myPageViewController.dataSource = ...)

@implementation MyPageViewController {
    // we keep our page view controllers in an array, but we could just as easily
    // instantiate them on the fly in the data source methods
    NSArray *_pages;
}

- (void)setupPages {
    /*
     * set up three pages, each with a different background color
     */
    UIViewController *a = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    a.view.backgroundColor = [UIColor redColor];
    UIViewController *b = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    b.view.backgroundColor = [UIColor greenColor];
    UIViewController *c = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    c.view.backgroundColor = [UIColor blueColor];
    _pages = @[a, b, c];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupPages];
    self.dataSource = self;
    // set the initially visible page's view controller... if you don't do this
    // you won't see anything.
    [self setViewControllers:@[_pages[0]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {
    }];
}

#pragma mark - UIPageViewControllerDataSource

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    if (nil == viewController) {
        return _pages[0];
    }
    NSInteger idx = [_pages indexOfObject:viewController];
    NSParameterAssert(idx != NSNotFound);
    if (idx >= [_pages count] - 1) {
        // we're at the end of the _pages array
        return nil;
    }
    // return the next page's view controller
    return _pages[idx + 1];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    if (nil == viewController) {
        return _pages[0];
    }
    NSInteger idx = [_pages indexOfObject:viewController];
    NSParameterAssert(idx != NSNotFound);
    if (idx <= 0) {
        // we're at the end of the _pages array
        return nil;
    }
    // return the previous page's view controller
    return _pages[idx - 1];
}

这篇关于如何在iOS 5中向UIPageViewController添加一个页面数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆