swiftnsbutton

NSButton state not changing


The following code works perfect in my sandbox:

@IBAction func filterClicked(sender: NSButton) {
  println(sender.state)
  if let clickEvent = NSApp.currentEvent! {
    if Int(clickEvent.modifierFlags.rawValue) & Int(NSEventModifierFlags.ControlKeyMask.rawValue) != 0 {
      if sender.state == NSOffState {
        sender.state == NSOnState
      }
    }
  }
  println(sender.state)
}

The connected button is an On-Off button. So when it's on and I ctrl-click it it will stay on.

Unfortunately in my app where I really need this it does not work. I checked that in both sandbox and prod app the bindings/settings are identically for both buttons. The debugger shows that

        sender.state == NSOnState

is simply not performed. state stays NSOffState. Gnawing my keyboard did not help. Any idea?


Solution

  • You are not assign any value to the button state.

    sender.state = NSOnState
    

    Update for Swift 4.2

    sender.state = NSControl.StateValue.on