gpt4 book ai didi

ios - Swift - UIPageViewController 总是重复第二个 ViewController

转载 作者:搜寻专家 更新时间:2023-10-31 22:16:52 25 4
gpt4 key购买 nike

设置:我正在尝试创建一个带有嵌入式 UIPageViewController 的 ViewController,其中有 4 个页面,每个页面都是自己的 ViewController。

问题:滚动时,第二个 View 总是重复出现。例如,对于 View1、View2、View3、View4,滚动会显示 View1、View2、View2、View3、View4,然后向后滚动会显示 View4、View3、View3、View2、View1 的顺序,即使页面控制是准确的。

我看了很多在线教程,包括答案 herehere .

这是我的页面 View Controller 类:

import UIKit

class HostGamePageViewController: ROLViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {

// Constants
let ViewControllers: NSArray = ["HostGameTest1", "HostGameTest2", "HostGameTest3", "HostGameTest4"]

// Properties
var pageViewController: UIPageViewController!
var currentViewController: String!

// UI
@IBOutlet var pageControl: UIPageControl!

override func viewDidLoad() {
super.viewDidLoad()

pageViewController = storyboard?.instantiateViewControllerWithIdentifier("HGPageViewController") as? UIPageViewController
pageViewController.delegate = self
pageViewController.dataSource = self

let startingViewController = viewControllerAtIndex(0)!
pageViewController.setViewControllers([startingViewController], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: {done in })

addChildViewController(pageViewController)
view.addSubview(pageViewController.view)
pageViewController.view.frame = view.bounds
pageViewController.didMoveToParentViewController(self)
view.gestureRecognizers = pageViewController.gestureRecognizers

// Setup page controls
pageControl.numberOfPages = ViewControllers.count
view.bringSubviewToFront(pageControl)
}

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

// Get the index of the current page view
func indexOfViewController() -> Int {
if currentViewController != nil {
return ViewControllers.indexOfObject(currentViewController)
}

return NSNotFound
}

// Get the view controller of the given index
func viewControllerAtIndex(index: Int) -> UIViewController? {
if ViewControllers.count == 0 || index >= ViewControllers.count {
return nil
}

pageControl.currentPage = index
currentViewController = ViewControllers[index] as String

return storyboard?.instantiateViewControllerWithIdentifier(currentViewController) as? ROLViewController
}

// Handle clicks on the page control
@IBAction func pageControlClick(sender: UIPageControl) {
var direction: UIPageViewControllerNavigationDirection

if sender.currentPage < indexOfViewController() {
direction = UIPageViewControllerNavigationDirection.Forward
} else {
direction = UIPageViewControllerNavigationDirection.Reverse
}

if let viewController = viewControllerAtIndex(sender.currentPage) {
pageViewController.setViewControllers([viewController], direction: direction, animated: false, completion: nil)
}
}

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
var index = indexOfViewController()

if index == 0 || index == NSNotFound {
return nil
}

index--
return viewControllerAtIndex(index)
}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
var index = indexOfViewController()

if index == NSNotFound {
return nil
}

index++
if index == ViewControllers.count {
return nil
}

return viewControllerAtIndex(index)
}

}

这是我的 Storyboard截图的链接: http://f.cl.ly/items/0V3W133Q34170q39393Q/Image%202014-09-11%20at%2011.49.44%20AM.png

我是否错误地设置了 dateSource?我在某处读到 UIPageViewController 通过将动画设置为 false 来进行一些缓存,这是应该的修复,但对我没有做任何事情。任何帮助将非常感激。谢谢!

最佳答案

我有一个非常相似的问题(不使用 swift,在 obj-c 中,有 3 个 View Controller ,中间的那个重复了一次:“0,1,1,2”)。原来这都是关于页面 View Controller 数据源方法的……

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController;
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController;

我还没有真正深入研究 swift(也没有兴趣这样做),但是伪读你的代码让我觉得你限制了“”的限制 before”和“after”关键方法(快速),变量包含在 Controller 内部用作UIPageViewControllerDataSource,即正是我更改并解决了问题。

我的英语不是很好,但我希望你能理解最后一句话。我使用的修复基于 this tutorial .在我的例子中,我在所有 3 种情况下都使用了 View Controller ,其中“UINavigationViewControllers”,所以我创建了一个具有属性的“NavigationWrapperViewController”:

@property(assign, nonatomic) NSUInteger index;

然后,在检查关键数据源方法中的边界限制时(只显示一个,对另一个使用相同的技术):

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
NSUInteger indice = ((NavigationWrapperViewController*)viewController).index;
if ((indice == 0) || (indice == NSNotFound)) {
return nil;
}
return [self viewControllerAtIndex:(--indice)];
}

并且在 viewControllerAtIndex 中实例化 View Controller 时,不要忘记在返回它们之前正确设置该属性:

NavigationWrapperViewController *wrapperViewController = [self.storyboard instantiateViewControllerWithIdentifier:self.ViewControllers[index]];
wrapperViewController.index = index;

return wrapperViewController;

细微的区别在于,显示的 View Controller 的索引由当前 View Controller 存储(使用一个简单的包装器,该类完全是为了这个目的而创建的,它只是在他的 .h 文件中有一个属性实现) UIPageViewControllerDataSource 负责的类。

这解决了我的问题,也许是某种线程相关的问题影响了当前获取的 View Controller 索引,并导致整个 - 重复 View Controller - 问题。

如果您愿意尝试,希望对您有所帮助!

关于ios - Swift - UIPageViewController 总是重复第二个 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25795093/

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