ios 无法将 UIViewController 类型的值转换为 PatternDetailViewController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31440964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't cast value of type UIViewController to PatternDetailViewController
提问by pdenlinger
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) "
我试图将视图控制器向下转换为详细视图控制器,但不能。我正在使用核心数据(第一次)。错误在 prepareForSegue 方法中,内容如下:“无法将类型为‘UIViewController’(0x1b81cdc)的值转换为‘Patternz.PatternDetailViewController’(0x32488)。(lldb)”
Would appreciate an explanation of why it doesn't work. Here are the files.
希望解释为什么它不起作用。这是文件。
ViewController.swift
视图控制器.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
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
}
}
回答by matt
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 reallyat the destination end of it.
错误消息告诉您destinationViewController
这个 segue 的实际上不是PatternDetailViewController。你可能认为它是,但它不是。你需要检查这个赛格瑞的故事板,看看什么是真正的在它的目的端。
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:
错误消息将其描述为 UIViewController 的事实让我怀疑您忘记在故事板的此视图控制器的身份检查器中输入任何视图控制器类型:
回答by Malloc
Adding to matt answer. Sometimes, you may also need to double check the custom class Modulefrom the Identity inspector.
添加到matt答案。有时,您可能还需要从身份检查器中仔细检查自定义类模块。
Your view controller need to belong to a specific Module
. So make sure to select a Module
from the list. In my case:
您的视图控制器需要属于特定的Module
. 所以一定Module
要从列表中选择一个。就我而言:
回答by Max
It looks like you are using a navigation controller judging by your viewDidLoad()
in PatternDetailViewController
.
根据您的viewDidLoad()
in判断,您似乎正在使用导航控制器PatternDetailViewController
。
If PatternDetailViewController
is embedded in a UINavigatonController
then the navigation controller will be segue.destinationViewController
.
如果PatternDetailViewController
嵌入在 a 中,UINavigatonController
则导航控制器将为segue.destinationViewController
.
Get the PatternDetailViewController like this:
像这样获取 PatternDetailViewController:
let vc: UINavigationController = segue.destinationViewController as! UINavigationController
let detailVC = vc.topViewController as! PatternDetailViewController
detailVC.pattern = self.selectedPattern
回答by Alaa Awad
This just happened to me and I struggled for a couple hours following all the other posts here. In the end it turned out that my destination view controller file was missing the .swift
extension. I hope this helps someone else!
这只是发生在我身上,我在这里的所有其他帖子后挣扎了几个小时。最后发现我的目标视图控制器文件缺少.swift
扩展名。我希望这对其他人有帮助!
回答by Onat Korucu
I solved the issue by following these steps:
我按照以下步骤解决了这个问题:
- Find the source Scene in the storyboard. Go to Identity Inspector (Third tab in the right panel)
- Remove (Cut/ctrl+x) the "Class" under "Custom Class" from the Identity inspector.
- Switch to another scene.
- Switch back to source scene.
- Paste the custom class module you just removed.
- Exit XCode.
- Open Xcode.
- Clean.
- Build.
- 在故事板中找到源场景。转到身份检查器(右侧面板中的第三个选项卡)
- 从身份检查器中删除(Cut/ctrl+x)“自定义类”下的“类”。
- 切换到另一个场景。
- 切换回源场景。
- 粘贴您刚刚删除的自定义类模块。
- 退出 XCode。
- 打开 Xcode。
- 干净的。
- 建造。
Why did it work? I was copy-pasting someone else's work. I probably wrote Class name in the right hand panel before there was a class with that name. My guess is Swift can not find these kinds of things dynamically. You have to behave like it wishes. You have to do certain things in a certain order.
为什么有效?我在复制粘贴别人的作品。在出现具有该名称的类之前,我可能在右侧面板中写了类名称。我的猜测是 Swift 无法动态找到这些东西。你必须表现得像它希望的那样。你必须按照一定的顺序做某些事情。
回答by William Hu
My issue is:
我的问题是:
Not work
不行
let aBCViewController = UIViewController(nibName: "ABCViewController", bundle: nil) as! ABCViewController
present(aBCViewController, animated: true, completion: nil)
Work
工作
let aBCViewController = ABCViewController(nibName: "ABCViewController", bundle: nil)
present(aBCViewController, animated: true, completion: nil)
回答by Ariven Nadar
This issue can happen if you copy Storyboardfiles from other project and did not change the Module.
如果您从其他项目复制Storyboard文件并且没有更改Module ,则可能会发生此问题。
In the above image "EmployeeApp" is the old project name and the "clientTradingApp" is the new project name
上图中“EmployeeApp”是旧项目名称,“clientTradingApp”是新项目名称
Happy Coding :)
快乐编码:)
回答by ninahadi
Try clean Product > Clean
尝试清洁产品 > 清洁
Edit: I came across the same error message and it was fixed using this method. This answer may not specifically answer the question
编辑:我遇到了相同的错误消息,并使用此方法修复了它。这个答案可能没有具体回答问题