尝试在pageViewController中以编程方式更改视图时崩溃

时间:2016-06-03 23:37:10

标签: ios swift uipageviewcontroller

我正在尝试以编程方式切换到pageViewController上的下一页。我正在使用单个contentViewController,我根据页面索引编辑内容。

以下是我的代码:

 override func viewDidLoad() {




    self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("RPageViewController") as! UIPageViewController

    self.pageViewController.dataSource = self

    let startVC = self.viewControllerAtIndex(0) as RatingContentView
    let viewControllers = NSArray(object: startVC)

    self.pageViewController.setViewControllers(viewControllers as? [UIViewController], direction: .Forward, animated: true, completion: nil)

    self.pageViewController.view.frame = CGRectMake(0, 30, self.view.frame.width, self.view.frame.size.height - 35)

    self.addChildViewController(self.pageViewController)
    self.view.addSubview(self.pageViewController.view)
    self.pageViewController.didMoveToParentViewController(self)

}



func viewControllerAtIndex(index: Int) -> RatingContentView{

    if ((self.pageTitles.count == 0) || (index >= self.pageTitles.count)) {

        return RatingContentView()
    }

    let vc: RatingContentView = self.storyboard?.instantiateViewControllerWithIdentifier("RatingContentView") as! RatingContentView


    if index != 0 {
        vc.name = self.arrayParticipants[index - 1]
        vc.pictureName = self.arrayPictureOfParticipants[index - 1]
    }
    vc.pageIndex = index
    vc.totalCount = self.arrayParticipants.count

    self.indexOfSlide = index

    return vc

}

  func setNextPage() {


    let startVC = self.viewControllerAtIndex(indexOfSlide + 1) as RatingContentView
    let viewControllers = NSArray(object: startVC)

    self.pageViewController.setViewControllers(viewControllers as? [UIViewController], direction: .Forward, animated: true, completion: nil)

      }

它不会在日志中打印任何错误,它只会崩溃并在此行上指出错误:         让vc:RatingContentView = self.storyboard?.instantiateViewControllerWithIdentifier(" RatingContentView")as! RatingContentView

我已经尝试了很多解决方案来解决这个问题,包括在viewDidLoad中添加上述内容,但都没有。 我试图在功能时转到下一张幻灯片       setNextPage() 被称为。

谢谢,

1 个答案:

答案 0 :(得分:0)

您是否尝试过检查故事板是否为零?我没看到你在哪里设置它。如果它是nil,你可能需要在调用instantiateViewControllerWithIdentifier之前实例化它:

let storyboard = UIStoryboard(name: "Main", bundle: nil)

另一件可以绊倒的事情是在这里或在故事板编辑器中拼错标识符(“RatingContentView”)。