简体   繁体   中英

Why am I getting this error after running my PageViewController?

I am looking to create a PageViewController very similar to SnapChat's one, whereby you can swipe from the UIImagePickerController to another VC. To do this, I have my initial VC which displays the imagepickercontroller, and a second VC (a caption VC) which I want to come after this initial VC. To encapsulate my PageViewController, I have created another VC class (shown below) which I have now set as my initial VC, and I am trying to handle the PageVC data source.

For whatever reason, it is not working and the error - 'fatal error: unexpectedly found nil while unwrapping an Optional value' occurs. Is this because you can't contain an imagePickerController in a PageVC (doubtful as SnapChat do). I created a simpler template which contained two simple VCs perfectly - why can I not do this here? The other one I did, I contained all the below code in the initial VC that the project starts with, whereas here I created an additional VC and manually changed it to make it the 'initial view controller'.

NB. the project compiles fine without the pageVC so it is nothing to do with any bad code in the other VCs.

I am very stuck and would hugely appreciate some help to this tricky issue. Thanks!

class PageViewController: UIViewController, UIPageViewControllerDataSource {

private var pageViewController: UIPageViewController?

private let VCarray = [ViewController(), CaptionViewController()]

override func viewDidLoad() {
    super.viewDidLoad()

    createPageViewController()

}

private func createPageViewController() {

    let pageController = self.storyboard!.instantiateViewControllerWithIdentifier("PageController") as! UIPageViewController
    pageController.dataSource = self

    if VCarray.count > 0 {
        pageController.setViewControllers([ViewController()], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
    }

    pageViewController = pageController
    addChildViewController(pageViewController!)
    self.view.addSubview(pageViewController!.view)
    pageViewController!.didMoveToParentViewController(self)

}

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {

    if viewController.isKindOfClass(CaptionViewController) {

        let pvc = self.storyboard?.instantiateViewControllerWithIdentifier("CameraVC")
        return pvc

    }

    return nil

}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {

    if viewController.isKindOfClass(ViewController) {

        let pvc = self.storyboard?.instantiateViewControllerWithIdentifier("CaptionVC")
        return pvc

    }

    return nil

}

The only optional unwrapping I see is on the first line. Is that where the exception is being thrown? Are you sure your main controller has a storyboard associated with it? If so, are you sure that the storyboard contains a controller named "PageController"?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM