Search code examples
swiftmacosnsbutton

NSButton programmatically Push on Push off Type


I am creating Buttons in Swift MacOS programmatically the following way:

        let feld = NSButton()
        feld.frame = CGRect(x: 900+30*h, y: 20, width: 22, height: 22)
        feld.image = NSImage(named: NSImage.Name("Mulleimer"))
        feld.target = self
        feld.action = #selector(ButtonTest)
        self.view.addSubview(feld)

Now I want to change the style of the button to "Push on Push off". I tried stuff like this:

feld.setButtonType(NSButton.???)

It just never works. I know the answer should be at:

https://developer.apple.com/documentation/appkit/nsbutton/1524983-setbuttontype

But dont know, I just dont understand how to transform this to working code. May anyone help? Thanks!


Solution

  • Please use code completion.

    Type setButtonType( and the dot and you will see

    enter image description here

    so it's simply

    feld.setButtonType(.pushOnPushOff)
    

    PS: Did you import AppKit?