简体   繁体   English

为什么在运行PageViewController后出现此错误?

[英]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. 我希望创建一个与SnapChat非常相似的PageViewController,您可以从UIImagePickerController滑动到另一个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. 为此,我有一个初始的VC,它显示了imagepickercontroller,另一个是我想在此初始VC之后使用的第二个VC(字幕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. 为了封装我的PageViewController,我创建了另一个VC类(如下所示),现在将其设置为我的初始VC,并且我试图处理PageVC数据源。

For whatever reason, it is not working and the error - 'fatal error: unexpectedly found nil while unwrapping an Optional value' occurs. 无论出于什么原因,它都无法正常工作,并且会出现错误-“致命错误:在展开可选值时意外发现nil”。 Is this because you can't contain an imagePickerController in a PageVC (doubtful as SnapChat do). 这是因为您不能在PageVC中包含imagePickerController(与SnapChat一样令人怀疑)。 I created a simpler template which contained two simple VCs perfectly - why can I not do this here? 我创建了一个更简单的模板,其中完美地包含了两个简单的VC-为什么在这里我不能这样做? 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'. 我做的另一个,我在项目开始的初始VC中包含了以下所有代码,而在这里,我创建了一个附加的VC,并手动对其进行了更改,使其成为“初始视图控制器”。

NB. 注意 the project compiles fine without the pageVC so it is nothing to do with any bad code in the other VCs. 该项目在没有pageVC的情况下可以正常编译,因此与其他VC中的任何错误代码无关。

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"? 如果是这样,您确定情节提要中包含一个名为“ PageController”的控制器吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我第二次UITableView搜索后会收到NSInternalInconsistencyException? - Why am I getting an NSInternalInconsistencyException after my second UITableView search? 释放对象后,为什么我仍然保持retainCount = 1 - Why am I still getting retainCount=1, after releasing my objects 为什么每次模拟器运行后都会出现 SIGTERM 错误? - Why am I getting SIGTERM error after every simulator run? 为什么在加载TableViewcontroller时出现此错误 - Why am I getting this error when loading my TableViewcontroller 为什么我的iOS模拟器中出现此Transform错误? - Why am I getting this Transform error in my iOS simulator? 为什么每次我的应用程序启动时都会出现“执行-layoutSubviews后仍然需要自动布局”错误? - Why am I getting a “Auto Layout still required after executing -layoutSubviews” error every time my app launches now? 我正在运行 flutter 应用程序并收到此错误 - I am running flutter app and getting this error 为什么在运行 appium 1.18.1 版时出现以下错误? - Why I am getting the following error while running appium version 1.18.1? 为什么我用cgcontext遇到此错误 - why am i getting this error with cgcontext 为什么我收到UnsafeMutablePointer错误? - Why am I getting an UnsafeMutablePointer error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM