PatternDetailViewController

PatternDetailViewController

我试图将 View Controller 转换为详细 View Controller ,但不能。我正在使用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
    }



}

最佳答案

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

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

错误消息告诉您,此命令的destinationViewController实际上不是PatternDetailViewController。您可能会认为是,但事实并非如此。您需要在 Storyboard 中检查此序列,并查看其最终目的是什么。

错误消息将其描述为UIViewController的事实使我怀疑您忘记在 Storyboard 中的此 View Controller 的身份检查器中输入任何 View Controller 类型:

关于ios - 无法将类型UIViewController的值转换为PatternDetailViewController,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31440964/

10-11 04:34