Adding Menu Item using XCode

Discussion in 'Mac Programming' started by satyam90, Oct 10, 2007.

  1. satyam90 macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #1
    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.:)
     
  2. garethlewis2 macrumors 6502

    Joined:
    Dec 6, 2006
    #2
    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.
     
  3. satyam90 thread starter macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #3
    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.
     
  4. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #4
    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?
     
  5. satyam90 thread starter macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #5
    I am using Cocoa and IDE XCode 2.4.1
    I am working on an Opensource project called "iFolder" please visit www.ifolder.com for project details.
     
  6. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #6
    In this case yes.

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

    [NSApp delegate];
     
  7. lucasgladding macrumors 6502

    Joined:
    Feb 16, 2007
    Location:
    Waterloo, Ontario
    #7
    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
     

Share This Page