设置:我试图创建一个ViewController,其中包含一个嵌入式UIPageViewController,其具有4个页面,每个页面都是它们自己的ViewController。
问题:当滚动时,第二个视图总是重复出现。例如,使用View1、View2、View3、View4,滚动会显示View1、View2、View2、View3、View4,然后向后滚动会有这个顺序View4、View3、View3、View2、View1,尽管页面控件是准确的。
我看了很多在线教程,包括这里和这里的答案。
以下是我的page view controller类:
这是我的故事板截图链接:http://f.cl.ly/items/0V3W133Q34170q39393Q/Image%202014-09-11%20at%2011.49.44%20AM.png 我是否设置了错误的数据源?我在某处读到,UIPageViewController通过将动画关闭来进行一些缓存,这是所谓的解决方法,但对我没有任何作用。非常感谢您的帮助。谢谢!
问题:当滚动时,第二个视图总是重复出现。例如,使用View1、View2、View3、View4,滚动会显示View1、View2、View2、View3、View4,然后向后滚动会有这个顺序View4、View3、View3、View2、View1,尽管页面控件是准确的。
我看了很多在线教程,包括这里和这里的答案。
以下是我的page 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)
}
}
这是我的故事板截图链接:http://f.cl.ly/items/0V3W133Q34170q39393Q/Image%202014-09-11%20at%2011.49.44%20AM.png 我是否设置了错误的数据源?我在某处读到,UIPageViewController通过将动画关闭来进行一些缓存,这是所谓的解决方法,但对我没有任何作用。非常感谢您的帮助。谢谢!