How to Setup Viewcontroller in Playgrounds

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:

Sample Image


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



Leave a reply



Submit