Cocoa: NSButton disappears after being pressed. Nevertheless,
I am writing a simple project in a C object using XCode 4.1, targeting macOS (not mobile) and am facing the following problem:
One of my buttons (NSButton sitting on top of NSView) will fade out periodically after being pressed. It is no longer drawn and the background is displayed in its place. This is not intentional behavior, and it only happens once in a while (perhaps one in twenty clicks). It is especially strange that the button is still active - the user can click in the space where the button should be displayed and the correct action will be taken and the button will be drawn again. Resizing the window also forces the button to paint again.
I tried the following fixes, none of which worked:
- Add a call to SetNeedsDisplay at the end of the click handling.
- Add a call to SetNeedsDisplay in an unrelated animation loop. It was a bad choice and caused the button to flicker and eventually resulted in a crash when using the button. With some sync, this might be a hack to work, but doesn't fix the root issue.
Any ideas on what is going on and how I can solve this problem? Thank!
edit: Still don't understand why this is happening, but I found a job: creating a duplicate button right below the one that disappears. Even when not drawn, clicks go to the top button. Kludge is accurate, but effective.
source to share
The most likely scenario is that both the button and the view it is in front of are not in the hierarchy, but have the same control. If so, then the behavior of the figure is undefined.
You need to make sure the button is a child of the view it is in front of. In code, you will do this by calling
, and in the interface builder, you will need to drag the button inside the containing view, not next to it.
source to share