【发布时间】:2015-07-07 03:23:23
【问题描述】:
感谢Using UIPageViewController with swift and multiple view controllers,我在UIPageViewController 中嵌入了导航控制器,因此我可以水平滚动浏览它们(如滑动导航)。问题:
当我到达第一个或最后一个导航控制器,然后向相反方向滑动时,第二个到第一个/最后一个导航控制器将复制。所以我将有 5 个导航控制器。例如:
从 FirstNav 开始,一直向左滑动到 FourthNav。当我从 FourthNav 向右滑动控制器阵列时,顺序将是:ThirdNav、ThirdNav、SecondNav、FirstNav。有谁能知道发生了什么?
class PageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
var index = 0
var identifiers: NSArray = ["FirstNav", "SecondNav", "ThirdNav", "FourthNav"]
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
self.delegate = self
let startingViewController = self.viewControllerAtIndex(self.index)
let viewControllers: NSArray = [startingViewController]
self.setViewControllers(viewControllers as [AnyObject], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
}
func viewControllerAtIndex(index: Int) -> UINavigationController! {
if index == 0 {
return self.storyboard!.instantiateViewControllerWithIdentifier("FirstNav") as! UINavigationController
}
if index == 1 {
return self.storyboard!.instantiateViewControllerWithIdentifier("SecondNav") as! UINavigationController
}
if index == 2 {
return self.storyboard!.instantiateViewControllerWithIdentifier("ThirdNav") as! UINavigationController
}
if index == 3 {
return self.storyboard!.instantiateViewControllerWithIdentifier("FourthNav") as! UINavigationController
}
return nil
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
let identifier = viewController.restorationIdentifier
let index = self.identifiers.indexOfObject(identifier!)
//if the index is the end of the array, return nil since we dont want a view controller after the last one
if index == identifiers.count - 1 {
return nil
}
//increment the index to get the viewController after the current index
self.index = self.index + 1
return self.viewControllerAtIndex(self.index)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
let identifier = viewController.restorationIdentifier
let index = self.identifiers.indexOfObject(identifier!)
//if the index is 0, return nil since we dont want a view controller before the first one
if index == 0 {
return nil
}
//decrement the index to get the viewController before the current one
self.index = self.index - 1
return self.viewControllerAtIndex(self.index)
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return 0
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return 0
}
}
【问题讨论】:
-
可以分享你的项目吗?
-
@ndmeiri 我在一个新项目中重新测试了代码,使用完全相同的代码。如果你想做同样的事情,创建一个页面视图控制器和 4 个视图控制器。将每个 VC 嵌入到它们自己的导航控制器中,并确保为每个导航控制器提供故事板 ID。然后将页面视图控制器链接到它的正确类名。
-
好的,我现在会尝试重现该问题。您使用的是什么版本的 Xcode,以及您正在构建的 iOS 版本是什么?
-
分别是 6.4 和 8.4 :)
-
需要注意的是,如果页面视图控制器设置为页面卷曲过渡样式,则没有重复项。只是滚动样式的问题。
标签: ios swift uinavigationcontroller uipageviewcontroller