views:

372

answers:

3

I have a dialog with 1 or more buttons and want to be able to programmatically set the rightmost one be the default one, so that when the user presses the Enter or Return key it should respond as if the user clicked it.

I'm using [btn setKeyEquivalent:@"\r"] and that makes it work fine but I want the button to have the blueish look which isn't happening. What do I need to do?

+1  A: 

The documentation says:

Note that if you set the key equivalent to Return, that button becomes the default button.

Thus, you're doing it correctly. You could try this after setting the key equivalent:

[button setNeedsDisplay:YES];

This forces the button to redraw itself.

Georg
+1  A: 
[myWindow setDefaultButtonCell:[btn cell]]; // should do the trick.

See Apple's documentation for more details.

e.James
+4  A: 

I was missing to set the button bezel style properly and that's why it didn't look properly. I'm now setting the button as this:

[btn setBezelStyle:NSRoundedBezelStyle];

Then any of these work to set the default button:

[myWindow setDefaultButtonCell:[btn cell]];

or

[btn setKeyEquivalent:@"\r"]
carlosb