简体   繁体   English

从情节提要实例化的视图控制器投射失败

[英]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.

相关问题 单元测试从故事板中快速转换视图控制器不起作用 - Unit Test Swift- casting view controller from storyboard not working 从情节提要上的Xib实例化视图进行无限循环 - Infinite loop on instantiated view from xib on storyboard 如何从情节提要中实例化UIButton类中的视图控制器实例 - How to get instance of view controller in UIButton class which is instantiated from storyboard Swift Xcode错误:线程1信号SIGABRT“从情节提要中实例化了视图控制器,但未获得UITableView - Swift xcode error: Thread 1 signal SIGABRT "instantiated view controller from storyboard but didn't get a UITableView 运行时错误消息:loadView 实例化视图 controller,标识符 ViewController 来自 storyboard Main,但没有获得 TableView - Runtime Error message: loadView instantiated view controller with identifier ViewController from storyboard Main,but didn't get a TableView '实例化视图 controller 与标识符“”来自 storyboard“主”,但在尝试以模式方式呈现视图 controller 时没有获得 UITableView - 'instantiated view controller with identifier “” from storyboard “Main”, but didn't get a UITableView' while trying to present view controller modally 故事板“Main”中带有标识符“UIViewController-ZWG-5q-24I”的实例化视图控制器,但没有获得UITableView。 - instantiated view controller with identifier “UIViewController-ZWG-5q-24I” from storyboard “Main”, but didn't get a UITableView.' 实例化视图 controller 在 storyboard 从 storyboard 少视图 Z594C103F2C6E010C3D8AB059CF - Instantiate view controller created in storyboard from a storyboard less view controller 单元格未显示在从故事板实例化的集合视图中(未嵌入) - Cell not displayed in collection view instantiated from storyboard (not embedded) 无法从视图控制器转到故事板参考? - Unable to segue from View Controller to Storyboard Reference?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM