ios – 不能将UIViewController类型的值转换为PatternDetailViewController

前端之家收集整理的这篇文章主要介绍了ios – 不能将UIViewController类型的值转换为PatternDetailViewController前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图将视图控制器降级到详细视图控制器,但不能.使用核心数据(第一次).
错误在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'

错误消息告诉您,该segue的destinationViewController实际上不是一个PatternDetailViewController.你可能会认为是,但不是.你需要在故事板中检查这个段落,看看它的目的地是什么.

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

原文链接:https://www.f2er.com/iOS/330227.html

猜你在找的iOS相关文章