1
votes

I have two NSButtons with images for both their on states and off states. Only one should be active at a time; click one and then click the other to change a property back and forth.

The problem is that if I disable a button when it's clicked so it cannot be clicked again, then the image is dimmed when the button is disabled--and I don't want it dimmed, I just want to use the alternate image. On the other hand, if I just leave the button enabled, but programmatically just don't run any code when it is clicked, then there's a flashing effect as the mouse clicks--which is distracting, when the button should not do anything.

So I either need to prevent the button from being dimmed when it is disabled, or prevent it from changing the button appearance while the mouse button is held down.

After reading up, it sounds like I need to subclass NSButtonCell and override - (BOOL)imageDimsWhenDisabled to do the former. But I can't figure out exactly how to subclass it (what sort of NSButtonCell class I should inherit from) and if the "setCell" method of NSButton is enough to use the new NSButtonCell class, or if I need to subclass NSButton as well.

Some tips on that would be appreciated, or perhaps there's a completely different approach that would achieve my objectives.

3
Why don't you just set NSButtonCell imageDimsWhenDisabled to NO?Dustin
NSButton's cell needs downcasting from NSCell not NSButtonCell, and still, setting imageDimsWhenDisabled is dimming the button.bauerMusic

3 Answers

5
votes

Check this out:

[btnInfo.cell setImageDimsWhenDisabled:NO];

2
votes

When you want to disable it without changing appearance do this:

On MacOS - NSButton:

Only option is to subclass NSButton and override mouseDown function

class RadioButton: NSButton {
    override func mouseDown(with event: NSEvent) {}
}

On iOS - UIButton:

Simple disable UserInteraction

mybutton.isUserInteractionEnabled = false
0
votes

For a more up to date answer in Swift, this works for me:

(theButton.cell! as! NSButtonCell).imageDimsWhenDisabled = false