一尘不染

无法将类型UIViewController的值转换为PatternDetailViewController

swift

我试图将视图控制器转换为详细视图控制器,但不能。我正在使用Core
Data(第一次)。错误发生在prepareForSegue方法中,显示为:“无法将类型’UIViewController’(0x1b81cdc)的值强制转换为’Patternz.PatternDetailViewController’(0x32488)。(lldb)”

希望能解释为什么它不起作用。这是文件。

ViewController.swift

import UIKit
import CoreData

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!

    var patterns : [Pattern] = []

    var selectedPattern : Pattern? = nil


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.tableView.dataSource = self
        self.tableView.delegate = self

        createTestPatterns()

        var context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext!

        var request = NSFetchRequest(entityName: "Pattern")

        var results = context.executeFetchRequest(request, error: nil)

        if results != nil {
            self.patterns = results! as! [Pattern]
        }
    }

    func createTestPatterns() {
        var context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext!

        var pattern = NSEntityDescription.insertNewObjectForEntityForName("Pattern", inManagedObjectContext: context) as! Pattern
        pattern.name = "Dress Shirt"
        pattern.frontimage = UIImageJPEGRepresentation(UIImage(named: "examplePattern.jpg"), 1)
        context.save(nil)
    }


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.patterns.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = UITableViewCell()
        var pattern = self.patterns[indexPath.row]
        cell.textLabel!.text = pattern.name
        cell.imageView!.image = UIImage(data: pattern.frontimage)
        return cell
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.selectedPattern = self.patterns[indexPath.row]
        self.performSegueWithIdentifier("patternDetailSegue", sender: self)
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "patternDetailSegue" {
            var detailViewController = segue.destinationViewController as! PatternDetailViewController // Could not cast value of type 'UIViewController' to 'Patternz.PatternDetailViewController'
            detailViewController.pattern = self.selectedPattern
        }
    }



}

PatternDetailViewController.swift

import UIKit

class PatternDetailViewController: UIViewController {

    var pattern : Pattern? = nil

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        self.navigationItem.title = self.pattern!.name
    }



}

阅读 193

收藏
2020-07-07

共1个答案

一尘不染

正如您所说,问题在于以下几行:

if segue.identifier == "patternDetailSegue" {
    var detailViewController = segue.destinationViewController as! PatternDetailViewController 
    // Could not cast value of type 'UIViewController' to 'Patternz.PatternDetailViewController'

错误消息告诉您,destinationViewController此设置的实际上 不是
PatternDetailViewController。您可能会认为是,但事实并非如此。你需要检查这个赛格瑞的故事板,看看什么是 真正的 在它的目的端。

错误消息将其描述为UIViewController的事实使我怀疑您忘记在情节提要中的此视图控制器的身份检查器中输入任何视图控制器类型:

Xcode屏幕截图

2020-07-07