3

我已经花了几个小时在这上面,但无法让它工作。希望有人能帮助我。

我有一个 UIPageViewController,当我在应用程序启动时将它添加到我的默认视图时,它可以完美运行。这就是我所做的:

//Step 1
//Instantiate the UIPageViewController.
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl
                                                          navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

//Step 2:
//Assign the delegate and datasource as self.
self.pageViewController.delegate = self;
self.pageViewController.dataSource = self;

//Step 3:
//Set the initial view controllers.
ContentViewController *contentViewController = [[ContentViewController alloc] initWithNibName:@"ContentViewController" bundle:nil];
contentViewController.labelContents = [self.modelArray objectAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:contentViewController];
[self.pageViewController setViewControllers:viewControllers
                                  direction:UIPageViewControllerNavigationDirectionForward
                                   animated:NO
                                 completion:nil];


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

现在我想在我的初始视图中使用导航控制器,然后当用户单击按钮时,它将 UIPageViewController 推送到堆栈上。这也有效,但是当我处于横向模式时, UIPageViewController 只显示一页(水平拉伸)而不是两页,因为它应该是。我需要将设备旋转为纵向,然后返回横向模式以强制 UIPageViewController 显示两个页面。我将 UIPageViewController 添加到导航控制器:

[self.navController pushViewController:self.pageViewController animated:NO];

当我调试我的代码时,我发现没有导航控制器 UIPageViewController 的委托方法在启动时被调用。当我将 UIPageViewController 推到导航控制器上时,它们不会被调用,直到我旋转设备。

有谁知道如何解决这个问题?在此先感谢您提供任何帮助/提示。

4

1 回答 1

3

在这个线程的帮助下想出了这个。

当您通过选项字典创建 pageViewController 时,关键是在 pageViewController 上设置 spinLocation:

    options = [NSDictionary dictionaryWithObject: [NSNumber numberWithInteger:UIPageViewControllerSpineLocationMid]
                                          forKey: UIPageViewControllerOptionSpineLocationKey];

这个选项应该放在一个检查里面,看看当前的界面方向是什么。

在上述线程中,界面方向由三元运算符检查;我把它放在一个 if 语句中,这样我就可以利用同样的机会将一个额外的 UIViewController 添加到 viewControllers 数组上。

如果界面方向是纵向的,则在创建 pageViewController 时 'options' 仍然为 nil,因此它将继续以纵向模式创建它,只有一个 viewController。

这是我在 BookViewController 中的整个 viewDidLoad 代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    _modelController = [[ModelController alloc] init];

    DataViewController *startingViewController = [self.modelController viewControllerAtIndex:0
                                                                                  storyboard:self.storyboard];

    NSMutableArray *viewControllers = [NSMutableArray arrayWithObjects:
                                startingViewController,
                                nil];
    NSDictionary *options;
    if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {

        DataViewController *secondViewController = [self.modelController viewControllerAtIndex:1
                                                                                    storyboard:self.storyboard];

        [viewControllers addObject:secondViewController];

        options = [NSDictionary dictionaryWithObject: [NSNumber numberWithInteger:UIPageViewControllerSpineLocationMid]
                                              forKey: UIPageViewControllerOptionSpineLocationKey];
    }

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

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

    self.pageViewController.dataSource = self.modelController;

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

    // Set the page view controller's bounds
    self.pageViewController.view.frame = self.view.bounds;

    [self.pageViewController didMoveToParentViewController:self];

    // Add the page view controller's gesture recognizers to the book view controller's view so that the gestures are started more easily.
    self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;

}
于 2012-09-14T17:55:05.783 回答