在Swift中的PageViewController当前页面索引

我想获得一个pageViewController的当前索引,我不知道如何得到可见的页面索引。

func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool,previousViewControllers: [UIViewController],transitionCompleted completed: Bool) { // How to get it? } 

您可以使用didFinishAnimating,并将标签设置为viewcontrollers。 尝试这个

 func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) { if (!completed) { return } self.pageControl.currentPageIndex = pageViewController.viewControllers!.first!.view.tag //Page Index } 

Swift 3:完整的程序化的PageViewController示例来获取/设置当前页面索引而不用视图标记:

 enum PageViewType:String { case green = "greenView" case blue = "blueView" case red = "redView" } class MyPageViewController: UIPageViewController { private (set) lazy var orderedViewControllers:[UIViewController] = { return [self.newPageView(.green), self.newPageView(.blue), self.newPageView(.red) ] } var currentIndex:Int { get { return orderedViewControllers.index(of: self.viewControllers!.first!)! } set { guard newValue >= 0, newValue < orderedViewControllers.count else { return } let vc = orderedViewControllers[newValue] let direction:UIPageViewControllerNavigationDirection = newValue > currentIndex ? .forward : .reverse self.setViewControllers([vc], direction: direction, animated: true, completion: nil) } } override func viewDidLoad() { super.viewDidLoad() dataSource = self if let firstViewController = orderedViewControllers.first { setViewControllers([firstViewController], direction: .forward, animated: true, completion: nil) } } private func newPageView(_ viewType:PageViewType) -> UIViewController { let vc = self.storyboard?.instantiateViewController(withIdentifier: viewType.rawValue) return vc } } 

UIPageViewController数据源实现:

 extension MyPageViewController:UIPageViewControllerDataSource { func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? { let previousIndex = currentIndex - 1 guard previousIndex >= 0 else { return nil } guard orderedViewControllers.count > previousIndex else { return nil } return orderedViewControllers[previousIndex] } func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { let nextIndex = currentIndex + 1 guard orderedViewControllers.count != nextIndex else { return nil } guard orderedViewControllers.count > nextIndex else { return nil } return orderedViewControllers[nextIndex] } func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int { return orderedViewControllers.count } func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int { return currentIndex } } 

在Swift 3

覆盖UIPageViewControllerDelegate的didFinishAnimating函数,如下所示:

 func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) { if completed { if let currentViewController = pageViewController.viewControllers![0] as? WalkthroughContentViewController { pageControl.currentPage = currentViewController.index } } } 

WalkthroughContentViewController是UIPageViewController提供的UIViewController

请记住在WalkthroughContentViewController中保留一个索引variables。 另外,在viewDidLoad方法集中:

 delegate = self 

尝试一下..

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