【发布时间】:2018-10-14 17:28:26
【问题描述】:
我正在使用适用于 macOS 的 swift 4,我可以使用以下代码以编程方式显示另一个视图控制器:
@IBAction func showVC(_ sender: NSButton) {
let vc = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil).instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "MyVC")) as! NSViewController
self.presentViewController(vc, asPopoverRelativeTo: sender.bounds, of: sender, preferredEdge: .maxX, behavior: .transient)
}
现在我想从 NSWindowController 做同样的事情,但我得到以下“错误”:
value of type 'WindowController' has no member 'presentViewController'
还有其他方法可以实现吗?
下一次尝试
@IBAction func showVC(_ sender: NSToolbarItem) {
let vc = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil).instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "MyVC")) as! NSViewController
self.contentViewController?.presentViewController(x, asPopoverRelativeTo: sender.view!.bounds, of: sender.view!, preferredEdge: .maxX, behavior: .transient)
}
应用程序在第二行崩溃:
fatal error: unexpectedly found nil while unwrapping an Optional value
我检查以下内容:
print(sender.view?.bounds ?? "No bounds")
print(sender.view ?? "No view")
打印结果
No bounds
No view
但是为什么呢??
【问题讨论】:
-
WindowController 是 NSViewController 的子节点吗? NSViewController 有一个
presentViewController(_:asPopoverRelativeTo:of:preferredEdge:behavior:)方法。 -
你不应该将 ViewControllers 与 NSWindowController 一起使用。只需将 Windows 添加到您的 NSWindowController
-
解释。我的窗口控制器有一个带有项目的工具栏。其中一项在 Window Controller 类中有一个 IBAction,它应该显示一个弹出框。并针对这种情况寻找解决方案
-
以 let vc = NSStoryboard 开头的行没有意义。我给你一个提示:contentViewController。
-
感谢提示。我用我的下一次尝试和问题更新了我的第一篇文章:/
标签: swift macos cocoa nswindowcontroller