How can I set a VC as Liveview in Playgrounds?
Choose File > New > Playground and start with the MacOS Playground template called Single View. This will give you a view in a nib.
Now modify the code in the playground as follows:
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 { $0 is NSView }
let vc = ViewController()
vc.view = views[0] as! NSView
PlaygroundPage.current.liveView = vc
Run the playground and look in the Assistant Editor pane. You will see this:
EDIT It occurs to me that a more pleasant way to write this (placing the decision as to where to get its view in the hands of the view controller itself) would be as follows:
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 { $0 is NSView }
self.view = views[0] as! NSView
}
}
PlaygroundPage.current.liveView = ViewController()
Swift Playgrounds Class Viewcontroller has no member ?
Your current clearDrawView()
is indeed not a member but a nested function in viewDidLoad()
.
Moving the definition to the class level should help. However, you also have to define drawView
as a member (i.e. at class level) to make it visible to clearDrawView()
:
public class GameViewController : UIViewController {
let drawView = CanvasView() // <--- move it here from viewDidLoad()
public override func viewDidLoad() {
super.viewDidLoad()
// do the necessary stuff, including drawView setup
}
func clearDrawView() {
drawView.clearCanvas()
}
@objc func clearCanvasButtonPressed() {
clearDrawView() // note: not "GameViewController.clearDrawView()" because it's an instance method
}
}
For better understanding check Properties and Methods in Apple docs.
How to create the Delegate Design Pattern within an Xcode Playground in Swift 5?
make the delegate public
public protocol UIManagerDelegate
view.addSubview(: ) Not working in swift playgrounds
You are setting PlaygroundPage.current.liveView
to the local view
created in viewDidLoad
. This is not the same view of the view controller which is what you are adding the button to in the setupStartButton
.
I would remove the line let view = UIView(frame: CGRect(x: 0, y: 0, width: 524, height: 766))
then every reference to view
will be the view controller's view.
You should remove the playground code from the viewDidLoad
method. After the view controller class, you can create an instance of the view controller and setup the playground:
Something like:
class MyViewController: UIViewController {
// your code
}
PlaygroundPage.current.liveView = MyViewController()
Related Topics
Swift .Uppercasestring or .Lowercasestring Property Replacement
Is Self Captured Within a Nested Function
Nested Types Inside a Protocol
Swift Draw Shadow to a Uibezier Path
Where to Get Frame Size of Custom Uiview in Its Subclass
Check for Launching from Uilocalnotification in Swift
Swift3:How to Handle Precedencegroup Now Operator Should Be Declare with a Body
How to Subclass Custom Uiviewcontroller in Swift
How to Extract Image from Lplinkview in Linkpresentation Framework
Cannot Assign to Property: 'Self' Is Immutable, I Know How to Fix But Needs Understanding
How to Quit Swift Repl Without Using Ctrl-D
Add Placeholder to Uitextfield, How to Set the Placeholder Text Programmatically in Swift
Duplicating a Particle Emitter Effect in Sprite Kit
How to Highlight a Uitextview's Text Line by Line in Swift
Switch That Checks Nsindexpath's Row and Section
How to Call Initializer for Subclass of Generic Type