I need to create a checkbox programatically in Cocoa and when I try and make a button with ButtonType set to NSSwitchButton it displays the title I gave it but not checkbox. I think I am missing something but I can't find any resources about making things like checkboxes without using the XCode GUI. Can anyone link a good resource please. Also, I don't think posting my code would be to useful because it is all wrapped in a custom lisp API.
+2
A:
I don't think buttons are bezeled by default when created programmatically. Check the setBezelStyle:
method, as well as setBezeled:
and setBordered:
. One of those should give you what you want.
Alex
2009-09-11 13:17:01
A:
I had failed to execute setImagePosition properly and this was causing the checkbox not to display.
Mike2012
2009-09-11 19:38:14
A:
The question is a little old so you've probably already figured it out, but I found it while searching for this exact thing. Alex danced around the solution without actually providing it. So here, for Google and all mankind: how to programmatically create a checkbox in Cocoa.
NSRect frame;
frame.size.width = frame.size.height = 18;
NSButton *myCheckBox = [[NSButton alloc] initWithFrame:frame];
[myCheckBox setButtonType:NSSwitchButton];
[myCheckBox setBezelStyle:0]; //This is unnecessary. I include it merely to show that checkboxes don't have a bezel style.
[myView addSubView:myCheckBox];
James Williams
2009-10-15 21:09:07