NSSavePanel accessoryView: Why doesn't my button appear?

409 views Asked by At

I am creating a save panel with an accessoryView that contains a single checkbox. I can get it to work when I create the button using:

NSButton(checkboxWithTitle: "Check Me", target: self, action: #selector(checkBoxSelected))

But this gives me a warning that this particular NSButton initializer requires macOS 10.12 and I need to support 10.10.

Here's my savePanel setup:

@IBAction func save(_ sender: NSButton) {
    let savePanel = NSSavePanel()
    savePanel.accessoryView = accessoryView()
    savePanel.runModal()
}

And here's how I create my accessory view in Sierra

func accessoryView() -> NSView {
    let checkbox = NSButton(checkboxWithTitle: "Check Me", target: self, action: #selector(checkBoxSelected))
    let view = NSView(frame: NSRect(x: 0, y: 0, width: 400, height: 100))
    view.addSubview(checkbox)
    return view
}

But this doesn't work (the button doesn't appear)

func accessoryView() -> NSView {
    let checkbox = NSButton()
    checkbox.setButtonType(NSSwitchButton)
    checkbox.title = "Check Me"
    checkbox.state = 1
    checkbox.target = self
    checkbox.action = #selector(checkBoxSelected)

    let view = NSView(frame: NSRect(x: 0, y: 0, width: 400, height: 100))
    view.addSubview(checkbox)
    return view
}
1

There are 1 answers

0
MH175 On

Got it.

Need to use NSButton.init(frame:)

Thanks