tsellon
tsellon

Reputation: 2426

Cocoa: NSButton disappears after being clicked. Still active however

I'm writing a simple project in objective C using XCode 4.1, targeting MacOS (not mobile), and have run into the following problem:

One of my buttons (NSButton, sitting on top of stock NSView), will intermittently disappear after being clicked. It is no longer drawn, and the background is shown in its place. This is not the intended behavior, and only happens once in a while (maybe one out of twenty clicks). Particularly odd, is that the button is still active--the user can click on the space where the button should be displayed, and the correct action will be taken and the button will again be drawn. Resizing the window also causes the button to be drawn again.

I've attempted the following fixes, none of which worked:

  1. Add a SetNeedsDisplay call to the end of the click processing.
  2. Add a SetNeedsDisplay call to an unrelated animation loop. This was a poor choice, and caused the button to flicker, and eventually led to a crash when the button was used. With some synchronization this might be hackable into a work around, but doesn't address the root problem.

Any ideas on what's happening, and how I might address it? Thanks!

edit: Still don't really understand why this is happening, but I did find a work-around: Creating a duplicate button directly below the one that's disappearing. Even when it's not drawn, clicks get directed to the top button. Kludge for sure, but effective.

Upvotes: 2

Views: 1026

Answers (1)

Rob Keniger
Rob Keniger

Reputation: 46020

The most likely scenario is that both the button and the view it is in front of are not in a hierarchy but have the same superview. If that is the case then the drawing behaviour is undefined.

You need to ensure that the button is a child of the view it is in front of. In code you'd do this by calling [parentView addSubView:yourButton], and in interface builder you need to ensure that you drag the button inside the containing view, not beside it.

Upvotes: 3

Related Questions