Swift - 带有 4 个 UIViewController 的 UIPageViewController
Posted
技术标签:
【中文标题】Swift - 带有 4 个 UIViewController 的 UIPageViewController【英文标题】:Swift - UIPageViewController with 4 UIViewControllers 【发布时间】:2015-03-23 14:53:17 【问题描述】:我有一个带有 4 个视图控制器的应用程序,我需要在 UIPageViewController 中实现。我在情节提要中有 6 个场景:rootViewController、页面视图控制器和我需要在页面视图控制器中使用的其他视图控制器。
这是我的页面视图控制器类中的代码:
class ViewController: UIViewController, UIPageViewControllerDataSource
var contentPageRestorationIDs = [String]()
var pageViewController = UIPageViewController()
override func viewDidLoad()
super.viewDidLoad()
contentPageRestorationIDs = ["Page1", "Page2", "Page3", "Page4"]
// Do any additional setup after loading the view, typically from a nib.
pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as UIPageViewController
pageViewController.dataSource = self
var startingController: UIViewController = self.viewControllerAtIndex(0)!
pageViewController.setViewControllers([startingController], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
self.addChildViewController(self.pageViewController)
self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
func goToPreviousContentViewController()
var currentViewController: AnyObject = self.pageViewController.viewControllers[0]
var vcRestorationID = currentViewController.restorationIdentifier
var index = find(contentPageRestorationIDs, vcRestorationID!!)
var previousViewController: UIViewController = self.viewControllerAtIndex(index! - 1)!
pageViewController.setViewControllers([previousViewController], direction: UIPageViewControllerNavigationDirection.Reverse, animated: false, completion: nil)
func goToNextContentViewController()
var currentViewController: AnyObject = self.pageViewController.viewControllers[0]
var vcRestorationID = currentViewController.restorationIdentifier
var index = find(contentPageRestorationIDs, vcRestorationID!!)
var nextViewController: UIViewController = self.viewControllerAtIndex(index! - 1)!
pageViewController.setViewControllers([nextViewController], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> (UIViewController?)
var vcRestorationID = viewController.restorationIdentifier
var index = find(contentPageRestorationIDs, vcRestorationID!)
if(index == 0)
return nil
return viewControllerAtIndex(index! - 1)
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> (UIViewController?)
var vcRestorationID = viewController.restorationIdentifier
var index = find(contentPageRestorationIDs, vcRestorationID!)
if(index == contentPageRestorationIDs.count - 1)
return nil
return viewControllerAtIndex(index! + 1)
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> (Int)
return self.contentPageRestorationIDs.count
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> (Int)
return 0
func viewControllerAtIndex(index: Int)-> BaseContentViewController!
if(index >= contentPageRestorationIDs.count)
return nil
var contentViewController:BaseContentViewController = storyboard?.instantiateViewControllerWithIdentifier(self.contentPageRestorationIDs[index])! as BaseContentViewController
contentViewController.rootViewController = self
return contentViewController
它在行中崩溃:
var contentViewController:BaseContentViewController = storyboard?.instantiateViewControllerWithIdentifier(self.contentPageRestorationIDs[index])! as BaseContentViewController
编辑:
【问题讨论】:
发布崩溃日志会有帮助... 我在崩溃日志中没有任何错误,我在 Xcode 上有此消息。 (我更新帖子) 【参考方案1】:问题出在我没有从自定义类扩展的一个视图控制器中......对不起
【讨论】:
以上是关于Swift - 带有 4 个 UIViewController 的 UIPageViewController的主要内容,如果未能解决你的问题,请参考以下文章
Swift - 带有 4 个 UIViewController 的 UIPageViewController
Swift:使用 UIImageView 显示带有从数组 [0..4] 中获取的 URL 的图像
带有两个自定义单元格的 TableView 导致单元格重用问题(Swift 4)