Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

satyam90

macrumors regular
Original poster
Jul 30, 2007
242
0
Bangalore, India
Hi,

I am extending certain features to a project.
I added a sub menu Item to existing Menu Item.
But I am not able to catch the signal or message generated by clicking submenu item. I want to show a message box on clicking it. Can any body suggest me how add a event or delegate for that submenu item click?

May be small thing for few people and as I am new to Mac programming, its a big task for me. I visited few websites and I couldn't get a solution for my problem.

Please help me.

Regards,
Satyam.:)
 
Here is one solution.

It assumes you have an app controller object. Create a input event in IB that responds to the message you want to process, e.g. handleMenuPush(id):sender or something like that. You will need to regenerate your header files in IB and put them in XCode.

Next control click from the menu item you want the message from to the controller object and select the handleMenuPush item. These are now connected.

Write the code behind handleMenuPush. Write something silly like NSLog(@"I got selected");

Run the program in XCode, marvel at the results as the test I got selected appear in the console.

As I said that is one way of doing it.
 
Hi garethlewis2,

Happy to see ur reply.
But I don't know how to create a new event. It is same as action?
How to find out which file is acting as app controller?
I am not able to do "Control click" on menu item. Nothing takes place on control click....
Can you please respond without feeling trouble for you.
regards,
Satya.
 
Hi,

I am extending certain features to a project.
I added a sub menu Item to existing Menu Item.
But I am not able to catch the signal or message generated by clicking submenu item. I want to show a message box on clicking it. Can any body suggest me how add a event or delegate for that submenu item click?

May be small thing for few people and as I am new to Mac programming, its a big task for me. I visited few websites and I couldn't get a solution for my problem.

Please help me.

Regards,
Satyam.:)

It would be helpful if you could tell us what kind of project you are extending. Is it Carbon? Is it Cocoa? Is it any other language?
 
But I don't know how to create a new event. It is same as action?

In this case yes.

I am not able to do "Control click" on menu item. Nothing takes place on control click....

In Interface builder you need to control click and drag the menu item to the controller to call the action.

How to find out which file is acting as app controller?

[NSApp delegate];
 
You should be able to catch the menu updates through:
Code:
- (int)numberOfItemsInMenu:(NSMenu *)menu
- (BOOL)menu:(NSMenu *)menu updateItem:(NSMenuItem *)item 
     atIndex:(int)index shouldCancel:(BOOL)shouldCancel

Return yes from the menu:updateItem:atIndex:shouldCancel: to continue the updates as per the documentation. Take a look at the NSMenu documentation for more info.

You can recreate the menu items using:
Code:
- (void)removeItemAtIndex:(int)index
- (void)insertItem:(id <NSMenuItem>)newItem atIndex:(int)index

Assign the delegate through Interface Builder or setDelegate:. You can use whatever object you want as the controller, but it's better to keep the methods in a specialized object - easier when the code grows.

If you are looking for a good Cocoa resource, take a look at this one:
http://www.amazon.com/exec/obidos/ASIN/0321213149/bignerdranch-20


Luke Gladding
www.hourlyapp.com
 
As an Amazon Associate, MacRumors earns a commission from qualifying purchases made through links in this post.
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.