1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

NSPopupButton selected item

Discussion in 'Mac Programming' started by Dreamspinner, Dec 17, 2012.

  1. macrumors member

    In my OS X Cocoa app, I'm using an NSPopupButton to present the user with a list of choices. When an item is selected, it isn't marked.

      [baudButton removeAllItems];
      [baudButton addItemWithTitle:(@"Baud")];
      [baudButton addItemWithTitle:(@"600")];
      [baudButton addItemWithTitle:(@"1200")];
    How to fix it?
  2. macrumors 6502

    I am going to guess you are talking about a checkmark - for that, you can set the state of the NSMenuItem.
  3. macrumors 6502a

    You could try to jump start the process.

    [baudButton selectItemAtIndex:1]
    Later you can read the state with:

    [baudButton indexOfSelectedItem]
    Also, make sure you made a connection to the right thing with Xcode. You could have made a connection to a specific cell instead of the whole pop-up button.
  4. macrumors member

    Thanks. Yes, a check mark would be good. I'll have to dig into how to use ValidateMenuItem I think. It seems like it's going to be a little messy if the user changes selection, and I have to find the checked item and un-set it, the set the new one. I sort of bogggled that it doesn't happen automagically:(. But I'm a newbie to Cocoa.
  5. macrumors member

    Thanks. I think we have a misunderstanding :confused:. I have no problem getting the selected menu item. I want to mark it as selected (check mark, change color, highlight the item).

    The connection is to the whole button.
  6. macrumors 6502

    There can be multiple checked items (and ways to use them), so what gets checked (or not) is up to you. For a single item you would be keeping track of the selection anyway, so typically your action would uncheck the current/old setting and check the new one - see Using Menu Item States.
  7. macrumors member

    Thanks for the URL. I'm sure that will come in handy!

    There are multiple items. Actually, setting the NSButton's type to popup accomplished my goal.

Share This Page