pollux
pollux

Reputation: 779

Cocoa: how to get type of button when I have an "id button" variable

I'm working on some code where I need to be able to get the type of button based on an "id button" variable. The button can be either a radio, checkbox or a plain pushbutton. The NSButton class has a setButtonType member but no feature to get the type of the button.

Upvotes: 3

Views: 286

Answers (2)

Mr. Berna
Mr. Berna

Reputation: 10655

The API doesn't give you that function directly.

A technique to give you this functionality is to give each button a tag based on the type of button. You can tag radio buttons with a 1, checkboxes with a 2 and pushbuttons with a 3 (or whatever works for you) at creation time. Tags can be assigned with Interface Builder or in code. Then your code can just check that tag and act appropriately.

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

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

Upvotes: 0

yuji
yuji

Reputation: 16725

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

The types available are for the most common button types, which are also accessible in Interface Builder. You can configure different behavior with the NSButtonCell methods setHighlightsBy: and setShowsStateBy:.

Note that there is no -buttonType method. The set method sets various button properties that together establish the behavior of the type.

If you really need to figure out the type of an arbitrary button, you'll need to make a table that determines the buttonType based on the possible values of highlightsBy and showsStateBy.

Upvotes: 1

Related Questions