1

How can a dynamically add (at run-time) menu items in Objective C at run-time? An example would be to add 5 menu items for recent files.

A) How would the add menu item look?

B) How would I setup checking to see what menu item was clicked and get the index or identity?

Please don't suggest a non-dynamic solution like adding slots and hiding at run-time. I'm trying to figure out how to dynamically add menu items at run-time, which on some other platforms is rather trivial to do and I'm hoping to make that code work nicely on a Mac.

Add: Adding a menu item seems straightforward

NSMenuItem *item = [myMenu insertItemWithTitle:[NSString stringWithFormat:@"%blah"]];

But how does one get the event for a dynamically added menu item?

1 Answer 1

1

When u r adding item set a Tag for each item and pass action.

Check it-

 item = [myMenu addItemWithTitle:@"" action:@selector(HitMe:) keyEquivalent: @""];
[item setTag:10];

In Delegate-

-(void)HitMe:(id) sender{
    NSMenuItem * item = (NSMenuItem*)sender;
    int val1 = [item tag];
    printf("Value1==>%d", val1);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you kindly, wasn't aware of the tag property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.