Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

ios - Can't cast value of type UIViewController to PatternDetailViewController

Am trying to downcast a view controller to a detail view controller but can't. Am using Core Data (for the first time). The error is in the prepareForSegue method and reads: "Could not cast value of type 'UIViewController' (0x1b81cdc) to 'Patternz.PatternDetailViewController' (0x32488). (lldb) "

Would appreciate an explanation of why it doesn't work. Here are the files.

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
    }



}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem, as you have said, is in these lines:

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

The error message tells you that the destinationViewController of this segue is not, in fact, a PatternDetailViewController. You may think it is, but it isn't. You need to examine this segue in the storyboard and see what's really at the destination end of it.

The fact that the error message describes it as a UIViewController makes me suspect that you forgot to enter any view controller type in this view controller's Identity inspector in the storyboard:

Xcode Screenshot


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...