我有4个View Controllers和app,我需要在UIPageViewController中实现。我在故事板中有6个场景:我需要在页面视图控制器中使用的rootViewController,页面视图控制器和其他ViewControllers。
这是我的页面视图控制器类的代码:
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
编辑:
答案 0 :(得分:2)
问题在于一个View Controller我没有从自定义类扩展...对不起