Question

Can I use the item's tag attribute to store a special key/ID or is it meant for something else?

Example of intended use:

  - (void)awakeFromNib {

    [self.popup addItemWithTitle: [NSString stringWithFormat: @"dummy title" ]];
    [[self.popup lastItem] setTag: 1658 ];
  }

  - (IBAction)popupAction: (id)sender {

    [self someMethod: [sender selectedItem].tag];

  }
Was it helpful?

Solution

yeah the tag of all controls is free for your devious use, you may use different numbers for different items, or to identify them as a kind of item, it is up to you.

OTHER TIPS

As the documentation says:

You typically assign tags to menu items from Interface Builder, but you can also assign them programmatically using the setTag: method of NSMenuItem.

For further details, read the Application Menus and Pop-Up Lists and User Interface Validation guides. But the basic idea is that Cocoa doesn't care what you put there.

I believe the intended purpose is to let you loosely couple parts of your code together—the code that validates user actions doesn't have to know how your interface is designed, and your interface doesn't have to know anything about the validator; they just have to agree on a number. However, in modern Cocoa, it's just as easy, and even more loose, to just look at the action, so the tag is freed up for anything you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top