swift - 使用从 Storyboard实例化的 View Controller 进行转换失败

标签 swift cocoa nsstoryboard

我有一个 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/

相关文章:

objective-c - 如何将字符串部分与 Objective-C 中的 NSArray 值进行匹配

objective-c - 如何将KVC集成到MVC中?

swift - 在 Swift 中制作 NSWindowController Singleton 的正确方法是什么?

ios - 禁用 uicollectionview 水平滚动的外部平移手势

macos - WKWebView osx 设置背景颜色

swift - 在 OS X Storyboard中通过标识符实例化 Split View Controller 不会加载 View 层次结构

macos - 为什么 NSApplication sharedApplication mainMenu 返回 nil ?

ios - 如何获取核心数据并使用 NSPredicate Swift 对其进行过滤

swift - 判断字符串在特定位置是否有数字

使用数据快速保存base64音频文件说文件不存在