如何禁用ViewControllers的无限循环?

时间:2015-11-11 17:35:49

标签: ios swift uipageviewcontroller

我在网上找到了这个代码并进行了测试。它工作但它进入无限循环。我看到一些关于为页面索引创建新func的帖子。

链接到我找到代码的网站:http://samwize.com/2015/10/13/how-to-create-uipageviewcontroller-in-storyboard-in-container-view/

是否有人愿意编辑代码以便循环结束?

import UIKit

class MyPageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

    var pages = [UIViewController]()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.delegate = self
        self.dataSource = self

        let page1: UIViewController! = storyboard?.instantiateViewControllerWithIdentifier("page1")
        let page2: UIViewController! = storyboard?.instantiateViewControllerWithIdentifier("page2")
        let page3: UIViewController! = storyboard?.instantiateViewControllerWithIdentifier("page3")

        pages.append(page1)
        pages.append(page2)
        pages.append(page3)

        setViewControllers([page2], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
        let currentIndex = pages.indexOf(viewController)!
        let previousIndex = abs((currentIndex - 1) % pages.count)

        return pages[previousIndex]
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        let currentIndex = pages.indexOf(viewController)!
        let nextIndex = abs((currentIndex + 1) % pages.count)
        return pages[nextIndex]
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

3 个答案:

答案 0 :(得分:8)

页面视图控制器不应该换行。

您的viewControllerBeforeViewControllerviewControllerAfterViewController方法都是从第一个到最后一个或最后一个到第一个来写的,这是错误的。

只需将viewControllerBeforeViewController重写为递减currentIndex,并返回该视图控制器,除非它<0。如果它<0,则返回nil。

同样适用于viewControllerAfterViewController,增加currentIndex,如果是< pages.count,则返回该项,否则为无。

看看你是否可以编写该代码。

答案 1 :(得分:3)

Swift 3.0

var pages = [UIViewController]()

extension ViewController: UIPageViewControllerDataSource {
    func pageViewController(_ pageViewController: UIPageViewController,
                            viewControllerBefore viewController: UIViewController) -> UIViewController? {
        let currentIndex = pages.index(of: viewController)!
        let previousIndex = currentIndex - 1
        return (previousIndex == -1) ? nil : pages[previousIndex]
    }

    func pageViewController(_ pageViewController: UIPageViewController,
                            viewControllerAfter viewController: UIViewController) -> UIViewController? {
        let currentIndex = pages.index(of: viewController)!
        let nextIndex = currentIndex + 1
        return (nextIndex == pages.count) ? nil : pages[nextIndex]
    }
}

答案 2 :(得分:-1)

import UIKit

类MyPageViewController:UIPageViewController,UIPageViewControllerDataSource,UIPageViewControllerDelegate {

var pages = [UIViewController]()

override func viewDidLoad() {
    super.viewDidLoad()

    self.delegate = self
    self.dataSource = self

    let page1: UIViewController! = storyboard?.instantiateViewControllerWithIdentifier("page1")
    let page2: UIViewController! = storyboard?.instantiateViewControllerWithIdentifier("page2")
    let page3: UIViewController! = storyboard?.instantiateViewControllerWithIdentifier("page3")


    pages.append(page1)
    pages.append(page2)
    pages.append(page3)


    setViewControllers([page2], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
}

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {


    let currentIndex = pages.indexOf(viewController)!


    let previousIndex = abs((currentIndex - 1) % pages.count)


    if (previousIndex > 0)
    {
        return nil
    }



    return pages[previousIndex]
}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
    let currentIndex = pages.indexOf(viewController)!
    let nextIndex = abs((currentIndex + 1) % pages.count)


    if (nextIndex < 1)
    {
        return nil
    }

    return pages[nextIndex]

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}