gpt4 book ai didi

ios - pageViewController viewControllerAfter/Before dataSource 方法被调用两次

转载 作者:行者123 更新时间:2023-11-30 11:02:59 28 4
gpt4 key购买 nike

我想以类似寻呼机的方式显示一些数据,所以我想使用`UIPageViewController,这基本上就是我所做的:

struct ModelTest {
var number: Int
}


class PagerViewController: UIPageViewController, UIPageViewControllerDataSource {

var currentIndex = 0

var models: [ModelTest] = [
ModelTest(number: 1),
ModelTest(number: 2),
ModelTest(number: 3),
ModelTest(number: 4),
ModelTest(number: 5)
]

override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
if let firstModel = models.first {
setViewControllers([ModelViewController(firstModel)], direction: .forward, animated: true, completion: nil)
}
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let nextIndex = currentIndex + 1
guard nextIndex < models.count else {
return nil
}
self.currentIndex += 1
return ModelViewController(models[nextIndex])
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let previousIndex = currentIndex - 1
guard previousIndex >= 0 else {
return nil
}
self.currentIndex -= 1
return ModelViewController(models[previousIndex])
}

}

但是,由于某种原因,dataSource 方法viewControllerAfterviewControllerBefore 有时会被调用两次。您知道为什么以及如何解决这个问题吗?

感谢您的帮助。

最佳答案

由于您是“UIPageViewController”而不是“UIViewController”的子类,因此无需再次设置委托(delegate)。

删除下面的行,它应该可以正常工作。

self.dataSource = self

关于ios - pageViewController viewControllerAfter/Before dataSource 方法被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53082026/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com