How do I disable the inifinite loop of ViewControllers?

我的未来我决定 提交于 2019-12-10 18:55:15

问题


I found this code online and tested it. It worked but it goes in an infinite loop. I saw some post about creating a new func for the index of the pages.

Link to the website I found the code on: http://samwize.com/2015/10/13/how-to-create-uipageviewcontroller-in-storyboard-in-container-view/

Anyone willing to edit the code so the loop ends?

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

回答1:


Page view controllers are not supposed to wrap.

Your viewControllerBeforeViewController and viewControllerAfterViewController methods are both written to wrap around from the first to the last or last to first, which is wrong.

Simply rewrite viewControllerBeforeViewController to decrement currentIndex, and return that view controller unless it's <0. If it is <0, return nil.

Likewise for viewControllerAfterViewController, increment currentIndex, and if it's < pages.count, return that item, else nil.

See if you can write that code.




回答2:


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



回答3:


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)


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

}



来源:https://stackoverflow.com/questions/33656432/how-do-i-disable-the-inifinite-loop-of-viewcontrollers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!