NSController calling function in NSView

50 Views Asked by At

I am trying to make NSViewControler work with NSView. I can't get myView to call any function in myView(NSView).

myView?.myFunctionName does not work and for NSView I get a nil error when I try set backgoundColor (myview?.layer?.backgroundColor = .white)

class ViewController: NSViewController {

@IBOutlet weak public var myview: NSView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        myview?.wantsLayer = true
        myview?.layer?.backgroundColor = .white
        myview?.needsDisplay = true
    }

    override var representedObject: Any? {
        didSet {
            //Update the view, if already loaded.
        }
    }
}

import Cocoa

class myView: NSView {
    var backgroundColor: NSColor?
       
     override func draw(_ dirtyRect: NSRect) {
        print("draw")
         if let bg = backgroundColor {
           bg.setFill()
           dirtyRect.fill()
         } else {
           super.draw(dirtyRect)
           print("error:\(String(describing: backgroundColor))")
           self.changeBackgroundColor(color: .blue)
        }
     }

    func changeBackgroundColor(color: NSColor) {
      //  print("change background\( bounds.size)")
      //  backgroundColor = color
        var path = NSBezierPath()
        path = NSBezierPath(rect: NSRect(origin:CGPoint(x: 0, y: 0), size: bounds.size))
       // print("\(String(describing: path))")
        path.close()
        color.setFill()
        path.fill()
        setNeedsDisplay(self.bounds)
    }
}
0

There are 0 best solutions below