[英]Casting failure with a view controller instantiated from a storyboard
I have a view controller that instantiates a new window controller. 我有一个实例化新窗口控制器的视图控制器。 I need to pass an object to that window controller's view controller.
我需要将对象传递给该窗口控制器的视图控制器。 Here is what I have so far:
这是我到目前为止的内容:
let storyboard = NSStoryboard(name: "Main", bundle: nil)
if let windowController = storyboard.instantiateController(withIdentifier: "customerEditWindowController") as? NSWindowController
{
let viewController = windowController.contentViewController as! EditCustomerViewController
viewController.customer = customer // (An object)
windowController.window?.makeKeyAndOrderFront(self)
}
The window is displayed as expected and then Xcode traps into the debugger with the error: 该窗口将按预期显示,然后Xcode捕获到调试器中并显示错误:
Could not cast value of type 'NSViewController' (0x7fffd1dcc4e0) to 'Inventory2.EditCustomerViewController' (0x100011660).
无法将类型'NSViewController'(0x7fffd1dcc4e0)的值强制转换为'Inventory2.EditCustomerViewController'(0x100011660)。
I'm confused since my EditCustomerViewController extends NSViewController. 我很困惑,因为我的EditCustomerViewController扩展了NSViewController。
class EditCustomerViewController: NSViewController
1) Why can't NSViewController be cast to EditCustomerViewController? 1)为什么不能将NSViewController强制转换为EditCustomerViewController?
2) Is there a better way to get data into the new view controller? 2)是否有更好的方法将数据导入新的视图控制器?
这是一个黑暗的简单镜头,但是您是否在Interface Builder中设置了视图控制器的类名?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.