UIPageViewController:返回当前可见视图


89

您如何知道内显示的当前页面/视图是UIPageViewController什么?

我已经覆盖了viewDidAppear我的子视图的方法,以便他们在其viewDidAppear方法中向其父视图发送一个ID 。

但是,问题是这样的:我无法可靠地使用该ID作为显示页面的ID。因为如果用户翻页但中途决定停止翻页并将页面放回原处,viewDidAppear则会被调用。(该视图在卷曲的页面后面可见)。

也许我应该只在当前视图消失时才切换到新的ID。但是我想知道是否没有更简单的方法来返回当前可见的视图?


您是否尝试过使用viewDidAppear:animated:
直到

哦是啊。我确实使用过。我编辑了问题以纠正错误。
1

您编辑问题的方式对我来说毫无意义。您可以从viewWillAppear或viewDidAppear中发送该ID。请重新检查您的修改。
直到

抱歉,我在编辑问题上做得不好。我一直使用viewDidAppear。我希望我的上一次编辑可以澄清这一点。
1

看看这里的家伙,这对我
有用

Answers:


103

您应该手动跟踪当前页面。委托方法pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted:将告诉您何时更新该变量。该方法的最后一个参数transitionCompleted:可以告诉您用户是否完成了翻页转换。


1
+1获得正确答案-我希望我有机会使用UIPageViewController并仅支持iOS5。
直到

谢谢,这正是我一直在寻找的干净解决方案。比网页上的代表更好。
1

3
旋转设备时,如何跟踪当前页面?
萨蒂扬

@直到可以,只需将项目的最低版本设置为ios5.0
mtmurdock 2012年

83
您如何知道是增加还是减少当前页码?
shim 2014年

59

从iOS 6开始,我发现viewControllersUIPageViewController 的属性不断更新,因此它将始终保留代表当前页面的一个视图控制器,而没有其他任何东西。因此,您可以通过调用来访问当前页面viewControllers[0](假设您一次只显示一个视图控制器)。

viewController数组仅在页面“锁定”到位后才更新,因此,如果用户决定部分显示下一页,则除非他们完成转换,否则它不会成为“当前”页面。

如果要跟踪“页码”,请在通过UIPageViewController数据源方法创建视图控制器时为其分配一个索引值。


因此,例如:

-(void)autoAdvance
    {
    UIViewController *currentVC = self.viewControllers[0];
    NSUInteger currentIndex = [myViewControllers indexOfObject:currentVC];

    if ( currentIndex >= (myViewControllers.count-1) ) return;

    [self setViewControllers:@[myViewControllers[ currentIndex+1 ]]
        direction:UIPageViewControllerNavigationDirectionForward
        animated:YES
        completion:nil];
    }
-(NSInteger)presentationIndexForPageViewController:
                         (UIPageViewController *)pageViewController
    {
    // return 0;
    UIViewController *currentVC = self.viewControllers[0];
    NSUInteger currentIndex = [myViewControllers indexOfObject:currentVC];
    return currentIndex;
    }

但是请注意这是不可靠的。


2
我发现它是不可靠的。我找不到问题的根源。当动画结束时,我访问viewControllers属性并且获得了正确的视图控制器,但是在方法上旋转时spineLocationForInterfaceOrientation,我不再获得正确的视图控制器。因此,我坚持维护自己的currentPage属性,而不是始终依赖viewControllers
法比奥·奥利维拉

有趣的是,我会发挥它去看了一下它的行为在这些特殊情况
艾迪博尔哈

2
我还发现它是不可靠的。看来Apple只是在每次翻页时都调用viewControllerBeforeViewController或viewControllerAfterViewController,但不会更新UIPageViewController.viewControllers。我不知道他们如何继续设法弄错这些类型的事情,但他们确实如此。这确实增加了我的工作量,并且破坏了我的代码的清晰度(通常违反了DRY和其他原则)。
扎克·莫里斯

3
@ZackMorris。绝对不可思议,您说得对。这是一个“苹果恐怖时刻”。为什么哦,为什么它们不包含明显的功能,例如向右移动一页,当前当前在我的页面上等等,所以令人难以置信的是,您无法“获取正在访问的页面!!!”
Fattie 2014年

1
self.viewControllers [0]似乎对我来说很好。但是我只在初始化期间调用一次setViewControllers,其余的则留给自动化。(在iOS 8.4中进行了测试)
Bob Bob

45

不幸的是,以上所有方法对我没有帮助。不过,我已经通过使用标签找到了解决方案。可能不是最好的,但是它可以工作并希望它对某人有帮助:

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed 
{
    if (completed) {
        int currentIndex = ((UIViewController *)self.pageViewController.viewControllers.firstObject).view.tag;
        self.pageControl.currentPage = currentIndex;
    }
}

在Swift中:(感谢@Jessy

func pageViewController(pageViewController: UIPageViewController,
    didFinishAnimating finished: Bool,
    previousViewControllers: [UIViewController],
    transitionCompleted completed: Bool)
{
    guard completed else { return }
    self.pageControl.currentPage = pageViewController.viewControllers!.first!.view.tag
}

示例: gist


3
这似乎是最简单的方法,对我来说效果很好。我在添加视图控制器时设置了标签(例如,HelpPage1ViewController * page1 = [self.storyboard InstantiateViewControllerWithIdentifier:@“ page1”]; page1.view.tag = 0;)。然后,您可以为主视图控制器设置currentPage属性,以跟踪您的位置。确保不仅添加数据源,而且添加两个协议(“ <UIPageViewControllerDataSource,UIPageViewControllerDelegate>”),并将委托设置为self(“ self.pageViewController.delegate = self;”),否则将不会调用该方法。这让我有些困惑。
James Toomey 2014年

1
@VictorM此解决方案不适用于我。我有一个具有不同图像的pageViewController。每次滑动都会保持返回标签0,为什么会这样呢?在此先感谢
theDC

1
@VictorM我花了几天的时间试图保存索引,但是不小心找到了这个答案,我希望我可以投票1000次,谢谢!
Evgeniy Kleban '17

1
伟大的解决方案感谢您花费数小时尝试解决此问题
Vision Mkhabela

它仅返回0值。我在做错什么吗
N. Der

35

以Ole的答案为基础…

这就是我实现这4种方法来跟踪当前页面并将页面指示符更新为正确索引的方式:

- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController{

    return (NSInteger)[self.model count];

}

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController{

    return (NSInteger)self.currentIndex;
}

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

    SJJeanViewController* controller = [pendingViewControllers firstObject];
    self.nextIndex = [self indexOfViewController:controller];

}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{

    if(completed){

        self.currentIndex = self.nextIndex;

    }

    self.nextIndex = 0;

}

2
我认为最后一行应该self.nextIndex = self.currentIndex不是self.nextIndex = 0为了在过渡未完成时正确还原索引。否则currentIndex,当您快速在页面中间某处来回分页时,可能会面临最终值为0的风险。
hverlind 2014年

1
这对我来说并不完全有效。有时,不会调用pageViewController:willTransitionToViewControllers,因此nextIndex不会更改,因此当前索引错误。
Hayden Holligan 2015年

31

下面的解决方案为我工作。

通过使本机UIPageViewController滚动视图分页更易于配置,Apple可以避免很多麻烦。我不得不求助于覆盖新的UIView和UIPageControl,因为本机UIPageViewController分页将不支持透明背景或在视图框架内重新定位。

- (void)pageViewController:(UIPageViewController *)pvc didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
  if (!completed)
  {
    return;
  }
  NSUInteger currentIndex = [[self.pageViewController.viewControllers lastObject] index];
  self.pageControl.currentPage = currentIndex;
}

1
在这里,您指的是指数吗?
Yohan

我正在获取索引(即表示self.pageViewController.viewControllers数组中当前视图控制器位置的整数),然后使用该索引来设置self.pageControl的currentPage属性(需要一个整数值) 。合理?
sirvine

6
索引在这里是自定义属性,我相信
Adam Johns 2014年

问题是,如果您要更改页面而不使用动画方法,则不会被调用
Silviu St

19

斯威夫特4

没有不必要的代码。3种方式。使用UIPageViewControllerDelegate方法。

func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    guard completed else { return }

    // using content viewcontroller's index
    guard let index = (pageViewController.viewControllers?.first as? ContentViewController)?.index else { return }

    // using viewcontroller's view tag
    guard let index = pageViewController.viewControllers?.first?.view.tag else { return }

    // switch on viewcontroller
    guard let vc = pageViewController.viewControllers?.first else { return }
    let index: Int
    switch vc {
    case is FirstViewController:
        index = 0
    case is SecondViewController:
        index = 1
    default:
        index = 2
    }
}

感谢上帝!我不知道pageViewController.viewControllers。你怎么知道viewControllers ..首先是那个问题?
凯蒂猫(Kitty)

实际上,相当简单,viewControllers数组仅包含当前提供的视图控制器。
尼古拉·米利切维奇

2
如何从该函数访问索引变量?
N. Der

11

我通过使用一个小函数并将pageIndex指定为静态NSInteger来跟踪页面索引。

-(void) setPageIndex
{
    DataViewController *theCurrentViewController = [self.pageViewController.viewControllers objectAtIndex:0];

    pageIndex = [self.modelController indexOfViewController:theCurrentViewController];
}

[self setPageIndex];在检测到方向变化后在Ole指定的函数内调用。


5

我最初使用Corey的解决方案,但该解决方案无法在iOS5上运行,然后最终使用,

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{

    if(completed) {
        _currentViewController = [pageViewController.viewControllers lastObject];
    }
}

它尝试切换不同的页面,目前效果很好。


3

不幸的是,以上对我来说都不起作用。

我有两个视图控制器,当我向后滚动最后一个视图时(大约20px),它将触发委托:

pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted:

并说当前页面(索引)0是错误的。

在子viewController中使用委托,如下所示:

- (void)ViewController:(id)VC didShowWithIndex:(long)page;

// and a property

@property (nonatomic) NSInteger index;

在内部触发,viewDidAppear例如:

- (void)viewDidAppear:(BOOL)animated
{
    ...

    [self.delegate ViewController:self didShowWithIndex:self.index];
}

为我工作。


3

这对我可靠

我有一个自定义UIPageController。此pageController.currentPage是从viewWillAppear中显示的UIViewController更新的

   var delegate: PageViewControllerUpdateCurrentPageNumberDelegate?

      init(delegate: PageViewControllerUpdateCurrentPageNumberDelegate ){
        self.delegate = delegate
        super.init(nibName: nil, bundle: nil)
      }

      required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
      }

    override func viewWillAppear(animated: Bool) {

        if delegate != nil {
          self.delegate!.upateCurrentPageNumber(0) //(0) is the pageNumber corresponding to the displayed controller
        }
      } 

    //In the pageViewController 

    protocol PageViewControllerUpdateCurrentPageNumberDelegate {

      func upateCurrentPageNumber(currentPageIndex: Int)
    }

     create the view display controllers initializing with the delegate

    orderedViewControllers = {
              return [
                IntroductionFirstPageViewController(delegate: self),
                IntroductionSecondPageViewController(delegate: self),
                IntroductionThirdPageViewController(delegate: self)
              ]

            }()

    the function implementing the protocol

    func upateCurrentPageNumber(currentPageIndex: Int){
        pageControl.currentPage = currentPageIndex
      }

1

我已经使用view.tag了一段时间,试图跟踪当前页面太复杂了。

在此代码中,索引存储在tag每个viewVC 属性中,并用于获取下一个或上一个VC。使用此方法,还可以创建无限滚动。查看代码中的注释以查看此解决方案:

extension MyPageViewController: UIPageViewControllerDataSource {

  func viewControllerWithIndex(var index: Int) -> UIViewController! {
    let myViewController = storyboard?.instantiateViewControllerWithIdentifier("MyViewController") as MyViewController

    if let endIndex = records?.endIndex {
      if index < 0 || index >= endIndex { return nil }
      // Instead, We can normalize the index to be cyclical to create infinite scrolling
      // if index < 0 { index += endIndex }
      // index %= endIndex
    }

    myViewController.view.tag = index
    myViewController.record = records?[index]

    return myViewController
  }

  func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
    let index = viewController.view?.tag ?? 0
    return viewControllerWithIndex(index + 1)
  }

  func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
    let index = viewController.view?.tag ?? 0
    return viewControllerWithIndex(index - 1)
  }

  func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
    return records?.count ?? 0
  }

  func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
    return (pageViewController.viewControllers.first as? UIViewController)?.view.tag ?? 0
  }
}

1

谢谢您的回答,我遇到类似的问题,不得不存储索引。我稍微修改了我的代码,将其粘贴在下面:

- (MenuListViewController *)viewControllerAtIndex:(NSInteger)index {

    if (_menues.count < 1)
        return nil;

    //    MenuListViewController *childViewController = [MenuListViewController initWithSecondSetFakeItems];
    MenuListViewController *childViewController = self.menues[index];
    childViewController.index = index;

    return childViewController;
}

#pragma mark - Page View Controller Data Source

- (void)pageViewController:(UIPageViewController *)pageViewController
        didFinishAnimating:(BOOL)finished
   previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers
       transitionCompleted:(BOOL)completed{

    if (completed) {

        NSUInteger currentIndex = ((MenuListViewController *)self.pageController.viewControllers.firstObject).index;
        NSLog(@"index %lu", (unsigned long)currentIndex);
    }
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSUInteger index = [(MenuListViewController *)viewController index];

    if (index == 0)
        return nil;

    index --;

    return [self viewControllerAtIndex:index];
}


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

    NSUInteger index = [(MenuListViewController *)viewController index];

    index ++;

    if (index == _menues.count)
        return nil;

    return [self viewControllerAtIndex:index];
}

0
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {

    NSLog(@"Current Page = %@", pageViewController.viewControllers);

    UIViewController *currentView = [pageViewController.viewControllers objectAtIndex:0];

    if ([currentView isKindOfClass:[FirstPageViewController class]]) {
             NSLog(@"First View");
        }
        else if([currentView isKindOfClass:[SecondPageViewController class]]) {
             NSLog(@"Second View");
        }
        else if([currentView isKindOfClass:[ThirdViewController class]]) {
              NSLog(@"Third View");
        }
}

//pageViewController.viewControllers always return current visible View ViewController

0

下面的演示代码(在Swift 2中)演示了如何通过实现简单的图像交换器教程来完成此操作。代码本身的注释:

import UIKit

/*
VCTutorialImagePage represents one page show inside the UIPageViewController.
You should create this page in your interfacebuilder file:
- create a new view controller
- set its class to VCTutorialImagePage
- sets its storyboard identifier to "VCTutorialImagePage" (needed for the loadView function)
- put an imageView on it and set the contraints (I guess to top/bottom/left/right all to zero from the superview)
- connect it to the "imageView" outlet
*/

class VCTutorialImagePage : UIViewController {
    //image to display, configure this in interface builder
    @IBOutlet weak var imageView: UIImageView!
    //index of this page
    var pageIndex : Int = 0

    //loads a new view via the storyboard identifier
    static func loadView(pageIndex : Int, image : UIImage) -> VCTutorialImagePage {
        let storyboard = UIStoryboard(name: storyBoardHome, bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier("VCTutorialImagePage") as! VCTutorialImagePage
        vc.imageView.image      = image
        vc.pageIndex            = pageIndex
        return vc
    }
}


/*
VCTutorialImageSwiper takes an array of images (= its model) and displays a UIPageViewController 
where each page is a VCTutorialImagePage that displays an image. It lets you swipe throught the 
images and will do a round-robbin : when you swipe past the last image it will jump back to the 
first one (and the other way arround).

In this process, it keeps track of the current displayed page index
*/

class VCTutorialImageSwiper: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

    //our model = images we are showing
    let tutorialImages : [UIImage] = [UIImage(named: "image1")!, UIImage(named: "image2")!,UIImage(named: "image3")!,UIImage(named: "image4")!]
    //page currently being viewed
    private var currentPageIndex : Int = 0 {
        didSet {
            currentPageIndex=cap(currentPageIndex)
        }
    }
    //next page index, temp var for keeping track of the current page
    private var nextPageIndex : Int = 0


    //Mark: - life cylce


    override func viewDidLoad() {
        super.viewDidLoad()
        //setup page vc
        dataSource=self
        delegate=self
        setViewControllers([pageForindex(0)!], direction: .Forward, animated: false, completion: nil)
    }


    //Mark: - helper functions

    func cap(pageIndex : Int) -> Int{
        if pageIndex > (tutorialImages.count - 1) {
            return 0
        }
        if pageIndex < 0 {
            return (tutorialImages.count - 1)
        }
        return pageIndex
    }

    func carrouselJump() {
        currentPageIndex++
        setViewControllers([self.pageForindex(currentPageIndex)!], direction: .Forward, animated: true, completion: nil)
    }

    func pageForindex(pageIndex : Int) -> UIViewController? {
        guard (pageIndex < tutorialImages.count) && (pageIndex>=0) else { return nil }
        return VCTutorialImagePage.loadView(pageIndex, image: tutorialImages[pageIndex])
    }

    func indexForPage(vc : UIViewController) -> Int {
        guard let vc = vc as? VCTutorialImagePage else {
            preconditionFailure("VCPagImageSlidesTutorial page is not a VCTutorialImagePage")
        }
        return vc.pageIndex
    }


    //Mark: - UIPageView delegate/datasource


    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        return pageForindex(cap(indexForPage(viewController)+1))
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
        return pageForindex(cap(indexForPage(viewController)-1))
    }

    func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [UIViewController]) {
        nextPageIndex = indexForPage(pendingViewControllers.first!)
    }

    func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if !finished { return }
        currentPageIndex = nextPageIndex
    }

    func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
        return tutorialImages.count
    }

    func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
        return currentPageIndex
    }

}

0

我有一个viewControllers数组,该数组显示在UIPageViewController中

extension MyViewController: UIPageViewControllerDataSource {

func presentationCount(for pageViewController: UIPageViewController) -> Int {
    return self.viewControllers.count
}

func presentationIndex(for pageViewController: UIPageViewController) -> Int {
    return self.currentPageIndex
}
}




extension MyViewController: UIPageViewControllerDelegate {

func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {

    if !completed { return }

    guard let viewController = previousViewControllers.last, let index = indexOf(viewController: viewController) else {
        return
    }

    self.currentPageIndex = index

}

fileprivate func indexOf(viewController: UIViewController) -> Int? {
    let index = self.viewControllers.index(of: viewController)
    return index
}
}

重要的是要注意到这里就是setViewControllers的方法UIPageViewController不给任何委托回调。委托回调仅表示UIPageViewController中的用户触摸操作。


0

这是我想出的解决方案:

class DefaultUIPageViewControllerDelegate: NSObject, UIPageViewControllerDelegate {

    // MARK: Public values
    var didTransitionToViewControllerCallback: ((UIViewController) -> Void)?

    // MARK: Private values
    private var viewControllerToTransitionTo: UIViewController!

    // MARK: Methods
    func pageViewController(
        _ pageViewController: UIPageViewController,
        willTransitionTo pendingViewControllers: [UIViewController]
    ) {
        viewControllerToTransitionTo = pendingViewControllers.last!
    }

    func pageViewController(
        _ pageViewController: UIPageViewController,
        didFinishAnimating finished: Bool,
        previousViewControllers: [UIViewController],
        transitionCompleted completed: Bool
    ) {
        didTransitionToViewControllerCallback?(viewControllerToTransitionTo)
    }
}

用法:

 let pageViewController = UIPageViewController()
 let delegate = DefaultUIPageViewControllerDelegate()

 delegate.didTransitionToViewControllerCallback = {
    pageViewController.title = $0.title
 }

 pageViewController.title = viewControllers.first?.title
 pageViewController.delegate = delegate

确保设置初始标题


0

解决此IMHO的最简单方法是使用PageControl存储转换的潜在结果,然后在取消转换的情况下还原。这意味着页面控件会在用户开始滑动时立即更改,这对我来说是可以的。这要求您拥有自己的UIViewControllers数组(在此示例中称为allViewControllers

func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
    if let index = self.allViewControllers.index(of: pendingViewControllers[0]) {
        self.pageControl.currentPage = index
    }
}

func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    if !completed, let previousIndex = self.allViewControllers.index(of: previousViewControllers[0]) {
        self.pageControl.currentPage = previousIndex
    }
}

0

如何viewController直接从UIPageViewController(Swift 4版本)请求一个:

fileprivate weak var currentlyPresentedVC: UIViewController?

func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    currentlyPresentedVC = pageViewController.viewControllers?.first
}

或者,如果您只需要在某个时间点使用当前提供的视图控制器,则只需pageViewController.viewControllers?.first在该时间使用即可。


0

在迅速的5和跟随sirvine的答复

extension InnerDetailViewController: UIPageViewControllerDelegate {

    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {

        if completed {
            guard let newIndex = embeddedViewControllers.firstIndex(where: { $0 == pageViewController.viewControllers?.last }) else { return }
            print(newIndex)
            currentEmbeddedViewControllerIndex = newIndex
        }

    }

} 

在这种情况下,我不在乎嵌入了什么类的UIViewController


-1
UIViewController *viewController = [pageViewController.viewControllers objectAtIndex:0];
NSUInteger currentIndex = [(ViewController*) viewController indexNumber];

它将返回当前页面索引。并且必须在UIPageViewController的委托函数(didFinishAnimating)下使用此代码。

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.