是否可以在UIPageViewController中以编程方式打开页面?


Answers:


235

是的,可以使用以下方法:

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

这与在页面上设置第一个视图控制器所使用的方法相同。同样,您可以使用它来转到其他页面。

想知道为什么viewControllers是数组而不是单个视图控制器?

这是因为页面视图控制器可能具有“书脊”(例如在iBooks中),一次显示2页内容。如果一次显示一页内容,则只需传入一个1元素的数组即可。

Swift中的示例:

pageContainer.setViewControllers([displayThisViewController], direction: .Forward, animated: true, completion: nil)

2
现在NDA已启动,可以进一步扩展吗?也许是代码示例。
SpaceTrucker 2011年

11
我想我终于明白了这一点:UIPageViewController并不真正在乎它当前在哪个页面上。调用setViewControllers时,它看起来好像是从当前的视图控制器以动画方式导航,但实际上并不是在寻找页面。
艾米2014年

3
示例:[self setViewControllers:@ [vcToMoveTo]方向:UIPageViewControllerNavigationDirectionForward动画:是完成:无];// vcToMoveTo是UIViewController
finneycanhelp 2014年

1
如果要继续使用UIPageControl,则可能需要:self.currentIndex = viewControllerToMoveTo的索引,然后为presentationIndexForPageViewController返回self.currentIndex
brendan

3
检查stackoverflow.com/questions/22554877/...如果您还需要更新的页面指标显示
Protongun

37

由于我也需要这样做,因此我将详细介绍如何执行此操作。

注意:我假设您使用标准模板形式来生成您的UIPageViewController结构- 在调用结构时同时具有modelViewControllerdataViewController。如果您不明白我写的内容,请回去创建一个新的项目,并UIPageViewController以此为基础。那你会明白的。

因此,需要翻转到特定页面涉及设置上面列出的方法的各个部分。对于本练习,我假设它是一个具有两个视图风景视图。 另外,我将其实现为IBAction,以便可以通过按下按钮或不通过按钮进行操作-使选择器调用并传递所需的项目也很容易。

因此,在此示例中,您将需要显示两个视图控制器-以及(可选)是向前还是向后。

请注意,我只是硬编码到第4和5页的位置,并使用向前滑动。从这里您可以看到,您需要做的就是传递变量,这些变量将帮助您获取这些项目。

-(IBAction) flipToPage:(id)sender {

    // Grab the viewControllers at position 4 & 5 - note, your model is responsible for providing these.  
    // Technically, you could have them pre-made and passed in as an array containing the two items...

    DataViewController *firstViewController = [self.modelController viewControllerAtIndex:4 storyboard:self.storyboard];
    DataViewController *secondViewController = [self.modelController viewControllerAtIndex:5 storyboard:self.storyboard];

    //  Set up the array that holds these guys...

    NSArray *viewControllers = nil;

    viewControllers = [NSArray arrayWithObjects:firstViewController, secondViewController, nil];

    //  Now, tell the pageViewContoller to accept these guys and do the forward turn of the page.
    //  Again, forward is subjective - you could go backward.  Animation is optional but it's 
    //  a nice effect for your audience.

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

    //  Voila' - c'est fin!  

}

2
我对移至特定页面有相同的要求。但是上面的代码似乎不适用于我。我究竟做错了什么?我有19页。
GaneshT 2011年

通过数据源对象提供视图控制器时,我们也可以使用此代码段吗?
詹斯·科尔

20
Apple,这是一个实施得很差的怪异课程。
devios1 2014年

29

这是单个页面视图的另一个代码示例。这里省略了viewControllerAtIndex的实现,它应返回给定索引的正确视图控制器。

- (void)changePage:(UIPageViewControllerNavigationDirection)direction {
    NSUInteger pageIndex = ((FooViewController *) [_pageViewController.viewControllers objectAtIndex:0]).pageIndex;

    if (direction == UIPageViewControllerNavigationDirectionForward) {
        pageIndex++;
    }
    else {
        pageIndex--;
    }

    FooViewController *viewController = [self viewControllerAtIndex:pageIndex];

    if (viewController == nil) {
        return;
    }

    [_pageViewController setViewControllers:@[viewController]
                                  direction:direction
                                   animated:YES
                                 completion:nil];
}

可以通过调用来更改页面

[self changePage:UIPageViewControllerNavigationDirectionReverse];
[self changePage:UIPageViewControllerNavigationDirectionForward];

但是页面索引控件不会更新。我怎么做?
thatzprem 2014年

1
这个答案对我有用-但是请注意,如果您依赖于UIPageViewController的“ didFinishAnimating”委托方法,则将不会触发它们。而是将您的代码从didFinishAnimating移到一个单独的方法,然后从setViewControllers方法的完成块中调用该方法。
DiscDev 2015年

1
@thatzprem如果要继续使用UIPageControl,则可能需要:self.currentIndex = viewControllerToMoveTo的索引,然后对于presentationIndexForPageViewController返回self.currentIndex
brendan

@thatzprem是正确的。每次进入同一页面,例如如果我们有3页,那么每次只会进入第二页。
Tushar Lathiya

18

我可能在这里完全错过了一些东西,但是这个解决方案似乎比其他许多提议要简单得多。

extension UIPageViewController {
    func goToNextPage(animated: Bool = true, completion: ((Bool) -> Void)? = nil) {
        if let currentViewController = viewControllers?[0] {
            if let nextPage = dataSource?.pageViewController(self, viewControllerAfter: currentViewController) {
                setViewControllers([nextPage], direction: .forward, animated: animated, completion: completion)
            }
        }
    }
}

1
很棒!只需还要以某种方式更改页面指示器。
TruMan1

真的很有帮助!请也给我们一个goToFirstPage的函数。
Mamta

每次进入同一页面,例如如果我们有3页,那么每次只会进入第二页。
Tushar Lathiya

15

这段代码对我来说很好用,谢谢。

这就是我所做的。有一些前进或后退的方法,以及一种直接进入特定页面的方法。纵向显示6页文档。如果将其粘贴到pageViewController模板的RootController的实现中,它将可以正常工作。

-(IBAction)pageGoto:(id)sender {

    //get page to go to
    NSUInteger pageToGoTo = 4;

    //get current index of current page
    DataViewController *theCurrentViewController = [self.pageViewController.viewControllers   objectAtIndex:0];
    NSUInteger retreivedIndex = [self.modelController indexOfViewController:theCurrentViewController];

    //get the page(s) to go to
    DataViewController *targetPageViewController = [self.modelController viewControllerAtIndex:(pageToGoTo - 1) storyboard:self.storyboard];

    DataViewController *secondPageViewController = [self.modelController viewControllerAtIndex:(pageToGoTo) storyboard:self.storyboard];

    //put it(or them if in landscape view) in an array
    NSArray *theViewControllers = nil;    
    theViewControllers = [NSArray arrayWithObjects:targetPageViewController, secondPageViewController, nil];


    //check which direction to animate page turn then turn page accordingly
    if (retreivedIndex < (pageToGoTo - 1) && retreivedIndex != (pageToGoTo - 1)){
        [self.pageViewController setViewControllers:theViewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];
    }

    if (retreivedIndex > (pageToGoTo - 1) && retreivedIndex != (pageToGoTo - 1)){
        [self.pageViewController setViewControllers:theViewControllers direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:NULL];
    }

}


-(IBAction)pageFoward:(id)sender {

    //get current index of current page
    DataViewController *theCurrentViewController = [self.pageViewController.viewControllers objectAtIndex:0];
    NSUInteger retreivedIndex = [self.modelController indexOfViewController:theCurrentViewController];

    //check that current page isn't first page
    if (retreivedIndex < 5){

        //get the page to go to
        DataViewController *targetPageViewController = [self.modelController viewControllerAtIndex:(retreivedIndex + 1) storyboard:self.storyboard];

        //put it(or them if in landscape view) in an array
        NSArray *theViewControllers = nil;    
        theViewControllers = [NSArray arrayWithObjects:targetPageViewController, nil];

        //add page view
        [self.pageViewController setViewControllers:theViewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];

    }

}


-(IBAction)pageBack:(id)sender {


    //get current index of current page
    DataViewController *theCurrentViewController = [self.pageViewController.viewControllers objectAtIndex:0];
    NSUInteger retreivedIndex = [self.modelController indexOfViewController:theCurrentViewController];

    //check that current page isn't first page
    if (retreivedIndex > 0){

    //get the page to go to
    DataViewController *targetPageViewController = [self.modelController viewControllerAtIndex:(retreivedIndex - 1) storyboard:self.storyboard];

    //put it(or them if in landscape view) in an array
    NSArray *theViewControllers = nil;    
    theViewControllers = [NSArray arrayWithObjects:targetPageViewController, nil];

    //add page view
    [self.pageViewController setViewControllers:theViewControllers direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:NULL];

    }

}

我想将其用于存在两个页面的横向模式。如何在那里使用它。可以请你指导。
iPhone以编程方式

9

斯威夫特4:

当我点击pagecontroller视图控制器上的next并更新pageControl索引时,我需要转到下一个控制器,所以这对我来说是最好,最直接的解决方案:

let pageController = self.parent as! PageViewController

pageController.setViewControllers([parentVC.orderedViewControllers[1]], direction: .forward, animated: true, completion: nil)

pageController.pageControl.currentPage = 1

5

如何使用dataSource中的方法?

UIViewController *controller = [pageViewController.dataSource pageViewController:self.pageViewController viewControllerAfterViewController:pageViewController.viewControllers.firstObject];
[pageViewController setViewControllers:@[controller] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];

类推后退。


3

Swift中

import UIKit

extension HomeViewController {

    // MARK: - Carousel management.

    func startTimerForMovingCarousel(let numberOfSecondsBetweenFiringsOfTheTimer: NSTimeInterval) {
        if (self.timerForMovingCarousel == nil) {
            self.timerForMovingCarousel = NSTimer.scheduledTimerWithTimeInterval(numberOfSecondsBetweenFiringsOfTheTimer,
                                                                            target: self,
                                                                            selector: "moveCarousel",
                                                                            userInfo: nil,
                                                                            repeats: true)
        }
    }

    func moveCarousel() {
        println("I move the carousel.")

        let currentContentViewControllerDisplayed = self.pageViewController!.viewControllers[0] as! ContentViewController

        var index: Int = currentContentViewControllerDisplayed.index

        if index == self.arrayViewControllers.count - 1 {
            index = 0
        } else {
            ++index
        }

        let contentViewControllerToDisplay = self.arrayViewControllers[index] as! ContentViewController

        self.pageViewController.setViewControllers([contentViewControllerToDisplay],
                                                    direction: UIPageViewControllerNavigationDirection.Forward,
                                                    animated: true,
                                                    completion: nil)

        self.pageControl.currentPage = contentViewControllerToDisplay.index
    }

    func invalidateTimerForMovingCarousel() {
        if (self.timerForMovingCarousel != nil) {
            self.timerForMovingCarousel.invalidate()
            self.timerForMovingCarousel = nil
        }
    }

}

1
你能给我完整的例子吗?因为我正在做这样的事情。如何在我的代码中使用此扩展名。如果您愿意,我可以将代码发送给您。
2015年

2

但是,使用此“ self.pageViewController setViewControllers”方法调用不会在已关闭页面的viewController上调用“ viewDidDissapear”,而手动打开页面会在已关闭页面上调用viewDidDissapear。我相信这是我坠机的原因

“提供的视图控制器的数量(0)与请求的转换所需的数量(1)不匹配”


尝试使用:– pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted:
Graham

2

我还需要一个按钮在PageViewController上向左导航,该按钮应能完全完成滑动动作所期望的操作。

由于我已经在“ pageViewController:viewControllerBeforeViewController ” 内部设置了一些规则来处理自然的滑动导航,因此我希望该按钮重新使用所有代码,并且像以前一样,无法使用特定的索引来访问页面回答了。因此,我不得不采取替代解决方案。

请注意,以下代码用于页面视图控制器,它是我的自定义ViewController中的一个属性,并且其主干线设置为中。

这是我为“向左导航”按钮编写的代码:

- (IBAction)btnNavigateLeft_Click:(id)sender {

    // Calling the PageViewController to obtain the current left page
    UIViewController *currentLeftPage = [_pageController.viewControllers objectAtIndex:0];

    // Creating future pages to be displayed
    NSArray *newDisplayedPages = nil;
    UIViewController *newRightPage = nil;
    UIViewController *newLeftPage = nil;

    // Calling the delegate to obtain previous pages
    // My "pageViewController:viewControllerBeforeViewController" method returns nil if there is no such page (first page of the book).
    newRightPage = [self pageViewController:_pageController viewControllerBeforeViewController:currentLeftPage];
    if (newRightPage) {
        newLeftPage = [self pageViewController:_pageController viewControllerBeforeViewController:newRightPage];
    }

    if (newLeftPage) {
        newDisplayedPages = [[NSArray alloc] initWithObjects:newLeftPage, newRightPage, nil];
    }

    // If there are two new pages to display, show them with animation.
    if (newDisplayedPages) {
        [_pageController setViewControllers:newDisplayedPages direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];
    }
}

您可以执行类似的操作以从此处创建一个右导航按钮。


2

在情况下,如果页面控件显示当前的页面是O &&你想要的页面通过滚动&也可通过点击页面视图控制器的自定义按钮浏览,下面可能会有所帮助。

//Set Delegate & Data Source for PageView controller [Say in View Did Load]
   self.pageViewController.dataSource = self;
   self.pageViewController.delegate = self;

// Delegates called viewControllerBeforeViewController when User Scroll to previous page 

 - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{

    [_nextBtn setTitle:@"Next" forState:UIControlStateNormal];

    NSUInteger index = ((PageContentViewController*) viewController).pageIndex;

    if (index == NSNotFound){
        return nil;
    }else if (index > 0){
        index--;
    }else{
        return nil;
    }        
    return [self viewControllerAtIndex:index];        
}

//当用户滚动到下一页时,委托名为viewControllerAfterViewController的代理

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{

    NSUInteger index = ((PageContentViewController*) viewController).pageIndex;

if (index == NSNotFound){
    return nil;
}else if (index < 3){
    index++;
}else{
    return nil;
}
return [self viewControllerAtIndex:index];}

//Delegate called while transition of pages. The need to apply logic in this delegate is to maintain the index of current page.

 - (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers{ 

buttonIndex = (int)((PageContentViewController*) pendingViewControllers.firstObject).pageIndex;

}

-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{    
if (buttonIndex == 0){
    _backButton.hidden = true;
}else if (buttonIndex == [self.pageImages count] - 1){
    _backButton.hidden = false;
    [_nextBtn setTitle:@"Begin" forState:UIControlStateNormal];
}else{
    _backButton.hidden = false;
}

}

//自定义按钮的(上一个和下一个)逻辑

-(void)backBtnClicked:(id)sender{
    if (buttonIndex > 0){
buttonIndex -= 1;
    }
    if (buttonIndex < 1) {
        _backButton.hidden = YES;
    }
    if (buttonIndex >=0) {
         [_nextBtn setTitle:@"Next" forState:UIControlStateNormal];
        PageContentViewController *startingViewController = [self viewControllerAtIndex:buttonIndex];
        NSArray *viewControllers = @[startingViewController];
        [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    }

}

-(void)nextBtnClicked:(id)sender{
if (buttonIndex < 3){
buttonIndex += 1;
}
if(buttonIndex == _pageImages.count){
   //Navigate Outside Pageview controller        
}else{        
    if (buttonIndex ==3) {

        [_nextBtn setTitle:@"Begin" forState:UIControlStateNormal];
    }
    _backButton.hidden = NO;

    PageContentViewController *startingViewController = [self viewControllerAtIndex:buttonIndex];
    NSArray *viewControllers = @[startingViewController];
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}

}

//BUTTON INDEX & MAX COUNT
-(NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController{
    return [self.pageImages count];}

-(NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController{
    return  buttonIndex;}

您已经对四个问题发表了几乎完全相同的答案。如果您认为其中一个是另一个的重复,则应将其标记为此类(并且不要多次发布相同的答案)。
哈普

因此,我可以在其他类似问题上发布此问题的链接,但解决方案相同。
Yogesh Lolusare,2016年

1
我并不是要判断,也不是要做什么,但是您的代码格式对我来说似乎很反感。
卢卡斯·彼得

1
对@Lukas,我现在将对其进行格式化。谢谢
Yogesh Lolusare

2
- (void)moveToPage {

    NSUInteger pageIndex = ((ContentViewController *) [self.pageViewController.viewControllers objectAtIndex:0]).pageIndex;

    pageIndex++;

    ContentViewController *viewController = [self viewControllerAtIndex:pageIndex];

if (viewController == nil) {
        return;
    }
    [self.pageViewController setViewControllers:@[viewController] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];

}

//现在调用此方法

[self moveToPage];

我希望它能起作用:)


1

对于单页,我只是编辑了@Jack Humphries的答案

-(void)viewDidLoad
{
  counter = 0;
}
-(IBAction)buttonClick:(id)sender
{
  counter++;
  DataViewController *secondVC = [self.modelController viewControllerAtIndex:counter storyboard:self.storyboard];
  NSArray *viewControllers = nil;          
  viewControllers = [NSArray arrayWithObjects:secondVC, nil];          
  [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];
}

0

正如@samwize指出的那样,分页方式是使用

setViewControllers:array

这是我的方法:我将数据源和委托分开。您可以调用此方法,仅更改“下一个”视图。以前,您必须应用分页规则。也就是说,您必须检查方向和下一个控制器。如果您将该方法与自定义数据源一起使用,则所显示的控制器数组将不会更改(因为将在NSObject子类上使用自定义数组)。

使用您自己的数据源,该方法仅设置一个新视图(具有特定方向)。由于您仍然可以控制正常滑动时将显示的页面。


0

从昨天开始我一直在研究它,我终于做到了。我不能完全使用标准模板,因为我有三种不同的viewControllers作为子视图:

1 - rootViewController;
2 - model;
3 - viewControllers
3.1 - VC1;
3.2 - VC2;
3.3 - VC3.
Note: all of VCs has Storyboard ID.

我只需要在第一个VC中使用触发器按钮,因此为pageViewController和model添加了一个属性:

#import <UIKit/UIKit.h>
#import "Model.h"

@interface VC1 : UIViewController

@property (nonatomic, strong) UIPageViewController *thePageViewController;
@property (nonatomic, strong) SeuTimePagesModel *theModel;

@end

我重写了模型的init方法以将Storyboard和pageViewController作为参数传递,因此可以将它们设置为VC1:

- (id)initWithStoryboard:(UIStoryboard *)storyboard
   andPageViewController:(UIPageViewController *)controller
{
    if (self = [super init])
    {
        VC1 *vc1 = [storyboard instantiateViewControllerWithIdentifier:@"VC1"];
        vc1.pageViewController = controller;
        vc1.model = self;

        VC2 *vc2 = [storyboard instantiateViewControllerWithIdentifier:@"VC2"];
        VC3 *vc3 = [storyboard instantiateViewControllerWithIdentifier:@"VC3"];
        self.pageData = [[NSArray alloc] initWithObjects:vc1, vc2, vc3, nil];
    }

    return self;
}

最后,我在vc1中添加了IBAction来触发pageViewController方法 setViewControllers:direction:animated:completion::

- (IBAction)go:(id)sender
{
    VC2 *vc2 = [_model.pages objectAtIndex:1];
    NSArray *viewControllers = @[vc2];
    [_pageViewController setViewControllers:viewControllers
                                  direction:UIPageViewControllerNavigationDirectionForward
                                   animated:YES
                                 completion:nil];
}

注意,我不需要找到VC索引,我的按钮将我带到了我的第二个VC,但是获取所有索引并跟踪您的子视图并不难:

- (IBAction)selecionaSeuTime:(id)sender
{
    NSUInteger index = [_model.pages indexOfObject:self];
    index++;

    VC2 *vc2 = [_model.pages objectAtIndex:1];
    NSArray *viewControllers = @[vc2];
    [_pageViewController setViewControllers:viewControllers
                                  direction:UIPageViewControllerNavigationDirectionForward
                                   animated:YES
                                 completion:nil];
}

希望对您有帮助!


0

这是使用UIPageViewControllerDataSource方法的解决方案:

该代码在UIPageViewController中跟踪当前显示的视图控制器。

...
@property (nonatomic, strong) UIViewController *currentViewController;
@property (nonatomic, strong) UIViewController *nextViewController;
...

首先将currentViewController初始化为UIPageViewController设置的初始视图控制器。

- (void) viewDidLoad {
    [super viewDidLoad];
    ...
    self.currentViewController = self.viewControllers[0];
    ...
    [self.pageViewController setViewControllers: @[self.currentViewController] direction: dir animated: YES completion: nil];
}

然后跟踪currentViewControllerUIPageViewControllerDelegate方法:pageViewController:willTransitionToViewControllers:pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted:


- (nullable UIViewController *) pageViewController: (UIPageViewController *) pageViewController viewControllerBeforeViewController: (UIViewController *) viewController {
    NSInteger idx = [self.viewControllers indexOfObject: viewController];
    if (idx > 0) {
        return self.viewControllers[idx - 1];
    } else {
        return nil;
    }
}

- (nullable UIViewController *) pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController: (UIViewController *) viewController {
    NSInteger idx = [self.viewControllers indexOfObject: viewController];
    if (idx == NSNotFound) {
        return nil;
    } else {
        if (idx + 1 < self.viewControllers.count) {
            return self.viewControllers[idx + 1];
        } else {
            return nil;
        }
    }
}

- (void) pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers {
    self.nextViewController = [pendingViewControllers firstObject];
}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed {
    if (completed) {
        self.currentViewController = self.nextViewController;
    }
}

最后,在您选择的方法中(在下面的示例中为-(IBAction)onNext:(id)sender),您可以使用UIPageViewControllerDataSouce方法pageViewController:viewControllerBeforeViewController:pageViewController:viewControllerAfterViewController:以编程方式切换到下一个或上一个控制器下一个或上一个视图控制器,并通过调用[self.pageViewController setViewControllers:@ [vc]方向:UIPageViewControllerNavigationDirectionForward动画:是完成:无]导航到该控制器


- (void) onNext {
    UIViewController *vc = [self pageViewController: self.tutorialViewController viewControllerAfterViewController: self.currentViewController];
    if (vc != nil) {
        self.currentViewController = vc;
        [self.tutorialViewController setViewControllers: @[vc] direction: UIPageViewControllerNavigationDirectionForward animated: YES completion: nil];
    }
}

详细的解释将非常有用。
Taslim Oseni '19

@TaslimOseni感谢您指出示例不够清楚。我添加了有关其工作原理的说明,如果需要更多说明,请告诉我。
pconor

0

由于ViewControllers嵌入在PageViewController中,因此您要做的就是:

  • 检索父视图控制器并将其强制转换为您的相关类。
  • 使用该父级数据源属性获取下一个ViewController。
  • 使用SetParentViewControllers方法设置并转换到下一页。

Xamarin中的示例,但在功能上与Swift等类似。以下代码将显示在一个ViewControllers中的Button Click委托中,显示为页面:

var parent = ParentViewController as WelcomePageViewController;
var next = parent.DataSource.GetNextViewController(parent, this);

parent.SetViewControllers(new UIViewController[] { next }, UIPageViewControllerNavigationDirection.Forward, true, null);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.