如何禁用 ViewControllers 的无限循环? [英] How do I disable the inifinite loop of ViewControllers?

查看:21
本文介绍了如何禁用 ViewControllers 的无限循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

推荐答案

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

Page view controllers are not supposed to wrap.

你的 viewControllerBeforeViewControllerviewControllerAfterViewController 方法都写成从第一个到最后一个或最后一个到第一个环绕,这是错误的.

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

只需重写 viewControllerBeforeViewController 以减少 currentIndex,并返回该视图控制器,除非它是 <0.如果它是 <0,则返回 nil.

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

同样对于viewControllerAfterViewController,增加currentIndex,如果它是<pages.count,返回那个项目,否则 nil.

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

看看你能不能写出那个代码.

See if you can write that code.

这篇关于如何禁用 ViewControllers 的无限循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆