Swift - UIPageViewController 总是重复第二个 ViewController [英] Swift - UIPageViewController always repeats second ViewController

查看:38
本文介绍了Swift - UIPageViewController 总是重复第二个 ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Setup: I'm trying to create a ViewController with an embedded UIPageViewController with 4 pages that are each their own ViewController.

问题:滚动第二个视图时总是重复.例如View1, View2, View3, View4 滚动显示View1, View2, View2, View3, View4 然后向后滚动显示这个顺序View4, View3, View3, View2, View1 即使页面控件是准确的.

Problem: When scrolling the second view is always repeated. For example with View1, View2, View3, View4 scrolling reveals View1, View2, View2, View3, View4 then scrolling back has this order View4, View3, View3, View2, View1 even though the page control is accurate.

我在网上看过很多教程,包括答案这里此处.

I've look at many tutorials online including answers here and here.

这是我的页面视图控制器类:

Here is my page view controller class:

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

}

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

and here is a link to a screenshot of my storyboard: http://f.cl.ly/items/0V3W133Q34170q39393Q/Image%202014-09-11%20at%2011.49.44%20AM.png

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

Am I setting up the dateSource incorrectly? I read somewhere that UIPageViewController does some caching by turning animation to false which is the supposed fix, didn't do anything for me. Any help would be much appreciated. Thanks!

推荐答案

我遇到了一个非常相似的问题(不使用 swift,在 obj-c 中,有 3 个视图控制器,中间的一个重复了一次:"0,1,1,2").原来这都是关于页面视图控制器数据源方法...

I've had a very similar problem (not using swift, in obj-c, had 3 view controllers and the one in the middle repeated one time: "0,1,1,2"). Turned out it was all about the Page View Controller Data Source methods...

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

我还没有真正深入研究 swift(也没有兴趣这样做),但是伪阅读你的代码让我觉得你限制了的限制before" 和 "after" 关键方法(swift-wise),在控制器中包含变量,用作 UIPageViewControllerDataSource,即正是我更改并解决了问题.

I haven't really digged into swift (nor had the interest in doing so yet), but pseudo-reading your code leaves me the idea that you restrict the limits of the "before" and "after" key methods (swift-wise) with variables contained inside the controller used as the UIPageViewControllerDataSource, that is exactly what I changed and fixed the issue.

我没有最好的英语,但我希望你能理解最后一句话.我使用的修复基于本教程.就我而言,我在UINavigationViewControllers"的所有 3 种情况下都使用了视图控制器,因此我创建了一个具有属性的NavigationWrapperViewController":

I don't have the best english but I hope you understood that last statement. The fix I used was based on this tutorial. In my case, the view controllers I used in all 3 cases where "UINavigationViewControllers", so I created a "NavigationWrapperViewController" that had a property:

@property(assign, nonatomic) NSUInteger index;

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

Then, when checking for the boundary limits in the key Data Source methods did (only showing one, use the same technique for the other one):

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

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

And when instantiating view controllers in viewControllerAtIndex, don't forget to set that property correctly before returning them:

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

return wrapperViewController;

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

The subtle difference is that the index of the view controller displayed is stored by the current view controller (using a simple wrapper, that class was made entirely with that purpose, it just has a property implementation in his .h file) instead of the UIPageViewControllerDataSource responsible class.

这解决了我的问题,也许是某种与线程相关的问题影响了当前获取的视图控制器索引,并导致了整个重复视图控制器问题.

That fixed my problem, maybe it was some sort of thread related issue that influenced on the current view controller index that was fetched and that caused the whole - repeating view controllers - problem.

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

Hope it helps if you are willing to try it out!

这篇关于Swift - UIPageViewController 总是重复第二个 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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