1

我有以下代码来创建 UIPageViewController:

- (void)viewDidLoad
{
[super viewDidLoad];

self.index = 0;

self.pageContainer = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

self.pageContainer.dataSource = self;
[[self.pageContainer view] setFrame:[[self view] bounds]];

DMSignUp1ViewController *first = [[DMSignUp1ViewController alloc] init];
DMSignUp2ViewController *second = [[DMSignUp2ViewController alloc] init];

self.viewControllers = [[NSArray alloc] initWithObjects:first, second, nil];

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

[self addChildViewController:self.pageContainer];
[[self view] addSubview:[self.pageContainer view]];
[self.pageContainer didMoveToParentViewController:self];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    if(self.index == 0) {
        return nil;
    }

    self.index--;
    return [self.viewControllers objectAtIndex:self.index];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    if(self.index == 1) {
        return nil;
    }

    self.index++;
    return [self.viewControllers objectAtIndex:self.index];
}

我有两个 UIViewControllers:DMSignUp1 和 2,我创建它们然后放置在 viewControllers 数组中。

当我运行它时,我收到以下错误:“提供的视图控制器数量 (2) 与请求的转换所需的数量 (1) 不匹配”

我的理解是因为我正在传递数组以用作视图控制器。那么如何设置我想使用数组中的第一个视图控制器呢?

4

2 回答 2

4

在 ViewController.m 文件末尾添加以下方法

- (DMSignUp1ViewController *)viewControllerAtIndex:(NSUInteger)index {

    DMSignUp1ViewController *first= [[DMSignUp1ViewController alloc] initWithNibName:@"APPChildViewController" bundle:nil];
    first.index = index;

    return childViewController;
}

并且,替换以下代码

DMSignUp1ViewController *first = [[DMSignUp1ViewController alloc] init];
DMSignUp2ViewController *second = [[DMSignUp2ViewController alloc] init];

self.viewControllers = [[NSArray alloc] initWithObjects:first, second, nil];

APPChildViewController *initialViewController = [self viewControllerAtIndex:0];

    NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];

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

希望这可以帮助你...

于 2013-07-24T17:25:51.757 回答
2

如果您将UIPageViewController spineLocation设置设置为除UIPageViewControllerSpineLocationMid使用时以外的任何设置,[self.pageContainer setViewControllers:则只能使用一个视图控制器传递一个数组。API 并不是很清楚,它说Controllers. 然后,您viewControllersUIPageViewController通过dataSource.

链接到脊椎参考

于 2013-07-24T15:50:07.743 回答