我有4个视图控制器的应用程序,我需要在UIPageViewController中实现。我在情节提要中有6个场景:rootViewController,Page View Controller和我需要在Page View Controller中使用的其他ViewController。

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

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

编辑:

最佳答案

问题是在一个View Controller中,我没有从自定义类中扩展...对不起

08-26 04:00