Cocoa: how to get button type when i have "id button" variable

I am working on some code where I need to get the button type based on the "id button" variable. The button can be either a radio, a checkbox, or a simple button. The NSButton class has a member setButtonType

but no function to get the type of a button.


source to share

2 answers

You can not. From the documentation you linked to, here's a bit on setButtontype


The available types are among the most common types of buttons that are also available in the Interface Builder. You can customize different behavior using the NSButtonCell


and methods setShowsStateBy:


Please note that there is no method -buttonType

. The set method sets various properties of the buttons, which together define the behavior of the type.

If you really need to figure out the type of the custom button, you need to create a table that determines buttonType

based on possible values highlightsBy

and showsStateBy




The API doesn't give you this function directly.

The method that will give you this function is to give each button a tag based on the button type. You can mark with radio buttons 1, checkboxes with 2 and buttons with 3 (or whatever works for you) during creation. Tags can be assigned using Interface Builder or code. Then your code can just check that tag and act accordingly.

// Where you make the button
[someRadioButton setTag:1];

// Where you just have and "id button"
if ([buttonId tag] == 1) {
    // Do radio button stuff




All Articles