【问题标题】:How to add an Array of pages to a UIPageViewController in iOS 5?如何在 iOS 5 中向 UIPageViewController 添加页面数组?
【发布时间】:2011-12-29 17:53:07
【问题描述】:

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

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

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。

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

【问题讨论】:

  • 你有没有想过这个问题?我遇到了同样的问题

标签: ios5 nsarray uipageviewcontroller uistoryboard


【解决方案1】:

您的设置方式与 UIPageViewController 的工作方式不同。

首先要了解的是setViewControllers:direction:animated:completion 只设置可见 视图控制器,而不是您可能希望向用户显示的所有视图控制器。 (也就是说,如果我尝试在您的代码中设置一个包含三个控制器的数组,则会出现异常。)

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

这是一个简短的示例(作为UIPageViewController 的子类实现,但也可以与子页面视图控制器一起使用...只需将self.dataSource = ... 替换为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];
}

【讨论】:

    【解决方案2】:

    我在这里看到了一些问题。我自己有点菜鸟,所以我不知道这是否会解决您代码中的所有问题。

    1. 您需要一个 UIPageViewController,然后在需要时实例化各个页面的视图。传递视图数组效率不高(占用太多内存)。

    2. 您需要在从 UIPageViewController 子类的类中实现 UIPageViewControllerDataSource 协议。您的 sn-p 中的大多数(但不是全部)代码都将放在此处。有关说明,请参阅 class referencethis tutorial

    3. 您使用手势识别器所做的工作可能比您需要的要多。如果您使用 IBOutlet,则可以通过界面生成器进行管道处理。

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      试试这个功能:

      setViewControllers:direction:animated:completion:
      

      设置要显示的视图控制器。

      - (void)setViewControllers:(NSArray *)viewControllers direction:(UIPageViewControllerNavigationDirection)direction animated:(BOOL)animated completion:(void (^)(BOOL finished))completion
      

      参数

      viewControllers:要显示的视图控制器或视图控制器。

      direction:导航方向。

      animated:一个布尔值,指示是否要对过渡进行动画处理。

      completion:翻页动画完成时调用的块。

      该块采用以下参数:

      完成 YES 如果动画完成;否,如果它被跳过。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-06-13
        • 1970-01-01
        • 2013-02-25
        • 2012-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多