我有一个 View Controller ,它实例化一个新的窗口 Controller 。 我需要将一个对象传递给该窗口 Controller 的 View Controller 。 这是我到目前为止所拥有的:
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)
}
窗口按预期显示,然后 Xcode 陷入调试器并显示错误:
Could not cast value of type 'NSViewController' (0x7fffd1dcc4e0) to 'Inventory2.EditCustomerViewController' (0x100011660).
我很困惑,因为我的 EditCustomerViewController 扩展了 NSViewController。
class EditCustomerViewController: NSViewController
1) 为什么 NSViewController 不能转换为 EditCustomerViewController?
2)是否有更好的方法将数据获取到新的 View Controller 中?
最佳答案
这是在黑暗中拍摄的一个简单镜头,但是您是否在 Interface Builder 中设置了 View Controller 的类名称?
关于swift - 使用从 Storyboard实例化的 View Controller 进行转换失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45121706/