如何在 Playgrounds 中将 VC 设为实时取景?
How can I set a VC as Liveview in Playgrounds?
我目前正在开发一个小型 Playgrounds 项目(适用于 macOS),我正在尝试将自己的视图控制器设置为实时视图。以下行不起作用。
PlaygroundPage.current.liveView = ViewController()
当运行这个时,我得到以下错误。
error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0).
我得到同样的错误,当使用这个时:
PlaygroundPage.current.liveView = ViewController(nibName: NSNib.Name("MyView"), bundle: Bundle.main)
您可能希望包括以下内容:
PlaygroundPage.current.liveView = ViewController()
PlaygroundPage.current.needsIndefiniteExecution = true
我将 link 附加到类似的问题:How to setup ViewController in Playgrounds?
让我知道是否有帮助!
你的错误是因为你的ViewController()
没有任何观点。这里的 link 到 documentation.
If you pass in a nil for nibNameOrNil then nibName will return nil and
loadView will throw an exception; in this case you must invoke
setView: before view is invoked, or override loadView.
一个可能的修复:
let myVC = NSViewController()
myVC.view = NSView()
PlaygroundPage.current.liveView = myVC
在第二种情况下,您是否像 documentation 所说的那样将 NIB 的文件所有者设置为当前视图控制器?
The specified nib file should typically have the class of the file's
owner set to NSViewController, or a custom subclass, with the view
outlet connected to a view.
选择“文件”>“新建”>“游乐场”,然后从名为“单一视图”的 MacOS 游乐场模板开始。这将为您提供笔尖视图。
现修改playground中的代码如下:
import AppKit
import PlaygroundSupport
class ViewController : NSViewController {}
let nibFile = NSNib.Name("MyView")
var topLevelObjects : NSArray?
Bundle.main.loadNibNamed(nibFile, owner:nil, topLevelObjects: &topLevelObjects)
let views = (topLevelObjects as! Array<Any>).filter { [=10=] is NSView }
let vc = ViewController()
vc.view = views[0] as! NSView
PlaygroundPage.current.liveView = vc
运行 游乐场,然后查看“助理编辑器”窗格。你会看到这个:
编辑 我想到一个更愉快的方式来写这个(将决定从哪里获得它的视图在视图控制器本身的手中)是如下:
class ViewController : NSViewController {
override func loadView() {
let nibFile = NSNib.Name("MyView")
var topLevelObjects : NSArray?
Bundle.main.loadNibNamed(
nibFile, owner:nil, topLevelObjects: &topLevelObjects)
let views = (topLevelObjects as! Array<Any>).filter { [=11=] is NSView }
self.view = views[0] as! NSView
}
}
PlaygroundPage.current.liveView = ViewController()
我目前正在开发一个小型 Playgrounds 项目(适用于 macOS),我正在尝试将自己的视图控制器设置为实时视图。以下行不起作用。
PlaygroundPage.current.liveView = ViewController()
当运行这个时,我得到以下错误。
error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0).
我得到同样的错误,当使用这个时:
PlaygroundPage.current.liveView = ViewController(nibName: NSNib.Name("MyView"), bundle: Bundle.main)
您可能希望包括以下内容:
PlaygroundPage.current.liveView = ViewController()
PlaygroundPage.current.needsIndefiniteExecution = true
我将 link 附加到类似的问题:How to setup ViewController in Playgrounds? 让我知道是否有帮助!
你的错误是因为你的ViewController()
没有任何观点。这里的 link 到 documentation.
If you pass in a nil for nibNameOrNil then nibName will return nil and loadView will throw an exception; in this case you must invoke setView: before view is invoked, or override loadView.
一个可能的修复:
let myVC = NSViewController()
myVC.view = NSView()
PlaygroundPage.current.liveView = myVC
在第二种情况下,您是否像 documentation 所说的那样将 NIB 的文件所有者设置为当前视图控制器?
The specified nib file should typically have the class of the file's owner set to NSViewController, or a custom subclass, with the view outlet connected to a view.
选择“文件”>“新建”>“游乐场”,然后从名为“单一视图”的 MacOS 游乐场模板开始。这将为您提供笔尖视图。
现修改playground中的代码如下:
import AppKit
import PlaygroundSupport
class ViewController : NSViewController {}
let nibFile = NSNib.Name("MyView")
var topLevelObjects : NSArray?
Bundle.main.loadNibNamed(nibFile, owner:nil, topLevelObjects: &topLevelObjects)
let views = (topLevelObjects as! Array<Any>).filter { [=10=] is NSView }
let vc = ViewController()
vc.view = views[0] as! NSView
PlaygroundPage.current.liveView = vc
运行 游乐场,然后查看“助理编辑器”窗格。你会看到这个:
编辑 我想到一个更愉快的方式来写这个(将决定从哪里获得它的视图在视图控制器本身的手中)是如下:
class ViewController : NSViewController {
override func loadView() {
let nibFile = NSNib.Name("MyView")
var topLevelObjects : NSArray?
Bundle.main.loadNibNamed(
nibFile, owner:nil, topLevelObjects: &topLevelObjects)
let views = (topLevelObjects as! Array<Any>).filter { [=11=] is NSView }
self.view = views[0] as! NSView
}
}
PlaygroundPage.current.liveView = ViewController()