如何在 iOS 5 中向 UIPageViewController 添加页面数组?

发布于 2024-12-15 21:14:15 字数 1457 浏览 1 评论 0原文

有谁知道我缺少什么才能将我自己的视图控制器手动添加到 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。

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

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];

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

Thanks to all of you who like to help eager newbies like me, in advance.... :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终遇你 2024-12-22 21:14:15

您的设置方式并不完全是 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];
}

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

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.)

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.

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];
}
与往事干杯 2024-12-22 21:14:15

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

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

  2. 您需要在 UIPageViewController 的子类中实现 UIPageViewControllerDataSource 协议。代码片段中的大多数(但不是全部)代码都将放在此处。请参阅类参考这个教程 进行解释。

  3. 您可能使用手势识别器完成了超出您需要的工作。如果您使用 IBOutlet,则可以通过界面生成器进行管道操作。

希望这有帮助。

I see a few problems here. I'm a bit of a noob myself, so I don't know if this will fix everything in your code.

  1. You need a UIPageViewController and then views for the individual pages that are instantiated when required. Passing an array of views isn't efficient (uses too much memory).

  2. You need to implement the UIPageViewControllerDataSource protocol in your a class that sub-classes from UIPageViewController. Most (but not all) of the code in your snippet will go here. See the class reference or this tutorial for an explanation.

  3. You're probably doing more work than you need to with the gesture recognizers. If you use IBOutlet, you can do the plumbing through the interface builder.

Hope this helps.

注定孤独终老 2024-12-22 21:14:15

尝试这个功能:

setViewControllers:direction:animated:completion:

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

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

参数

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

方向:导航方向。

动画:一个布尔值,指示过渡是否要动画。

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

该块采用以下参数:

finished
如果动画完成,则为 YES;如果被跳过则否。

Try this function:

setViewControllers:direction:animated:completion:

Sets the view controllers to be displayed.

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

Parameters

viewControllers: The view controller or view controllers to be displayed.

direction: The navigation direction.

animated: A Boolean value that indicates whether the transition is to be animated.

completion: A block to be called when the page-turn animation completes.

The block takes the following parameters:

finished
YES if the animation finished; NO if it was skipped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文