我正在尝试编译我的Xcode应用程序。它可以成功构建,但是不会超过启动屏幕。

另外,它一直使我引用以下特定代码行:instantiateViewControllerWithIdentifier(下面的完整代码),并且在日志中我得到以下消息:

Unknown class ContentViewController in Interface Builder file.
Could not cast value of type 'UIViewController' (0x10741f418) to 'HelloHi.ContentViewController' (0x1049aa2e0).
(lldb)


谁能帮我解决这个问题?

import UIKit

class ViewController: UIViewController, UIPageViewControllerDataSource {

    var pageViewController: UIPageViewController!
    var pageTitles: NSArray!
    var pageImages: NSArray!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.pageTitles = NSArray(objects: "Welcome", "Hello")
        self.pageImages = NSArray(objects: "page1", "page2")

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

        self.pageViewController.dataSource = self

        var startVC = self.viewControllerAtIndex(0) as ContentViewController
        var viewControllers = [startVC]

        self.pageViewController.setViewControllers(viewControllers, direction: .Forward, animated: true, completion: nil)

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

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

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func restartAction(sender: AnyObject) {

        var startVC = self.viewControllerAtIndex(0) as ContentViewController
        var viewControllers = [startVC]

        self.pageViewController.setViewControllers(viewControllers, direction: .Forward, animated: true, completion: nil)

    }

    func viewControllerAtIndex(index: Int) -> ContentViewController

    {

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

        var vc: ContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ContentViewController") as! ContentViewController

        vc.imageFile = self.pageImages[index] as! String
        vc.titleText = self.pageTitles[index] as! String
        vc.pageIndex = index

        return vc


    }

    //MARK: - Page View Controller Data Source

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

        var vc = viewController as! ContentViewController
        var index = vc.pageIndex as Int

        if (index == 0 || index == NSNotFound)
        {
            return nil
        }

        index--
        return self.viewControllerAtIndex(index)

    }

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

        var vc = viewController as! ContentViewController
        var index = vc.pageIndex as Int

        if (index == NSNotFound)
        {
            return nil
        }

        index++
        if (index == self.pageTitles.count)
        {
            return nil
        }

        return self.viewControllerAtIndex(index)

    }

    func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
        return self.pageTitles.count
    }

    func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
        return 0
    }

}

最佳答案

该错误消息表明Interface Builder文件中的视图控制器设置了错误的类。选择有问题的控制器,打开身份检查器,然后将类名称更改为正确的UIViewController子类。大概这是您的ContentViewController

当然,您需要在项目中的某个位置定义ContentViewController类。它不在您发布的代码中,并且在出现错误消息后,它不在项目中的其他任何位置。

关于ios - Xcode不会打开ContentViewController,但会停留在启动屏幕上,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30144866/

10-09 16:34