using menu to do something

Discussion in 'Mac Programming' started by henryt, Oct 31, 2008.

  1. henryt macrumors newbie

    Oct 31, 2008
    I am using xcode 3.1.1 and use the document project in objec-C. How do i add a menu in the main menu and call a function in my doc nib, to do something, like zoon in and out a display?
  2. kainjow Moderator emeritus


    Jun 15, 2000
    Open the MainMenu nib file in Interface Builder. Within it is the MainMenu menu bar that you can edit.

    You can also edit it in code via [NSApp mainMenu]
  3. henryt thread starter macrumors newbie

    Oct 31, 2008
    using menus

    I am working with a dcoument type application. i added a menu in menu nib and added an action to firstresponder. Then in the document nib, I added a custon view in the document. How do I intercept the just added menu in my custom view in document nib? It seems that I cannot go across nibs to access any objects? How this is done?
  4. Oligarch macrumors newbie

    Nov 10, 2008
    No need to "go across nibs" or "intercept" anything.

    In Interface Builder, open MainMenu.nib. Control-drag (holding down the control key, click, and then drag) from your menu item, say "Zoom", to the first responder. In the connections pane of the inspector window, select the action method you have added to first responder, say "zoom:", and connect. That's it in Interface Builder. (Don't forget to save.)

    In your custom view class, you programmatically (in Xcode, not in Interface Builder) implement a method called "zoom:", like

    - (IBAction)zoom:(id)sender {
        zoomFactor *= 2.0; [self setNeedsDisplay:YES];
    Finished. (Don't forget to save and build.)

    Now, when your application is running and you select "Zoom" from the menu, the application will send an action message with "zoom:" as the method to perform to whichever view happens to be the key view (to have input focus, the one on which you last clicked and to which keyboard input gets sent, often with a thin, light-blue frame around it).

    If the key view is an instance of your custom view class, it will "respond" to the action message, because you have implemented a "zoom:" method, which will then get performed.

    Otherwise, if the key view is of another class, a text field, for example, it will not respond to the "zoom:" message and nothing will happen.

    To summarize, the "intercepting" is done for you by the application at run-time, based on the name of the action method and the class of the key (input receiving) view.
  5. henryt thread starter macrumors newbie

    Oct 31, 2008

    Thanks I got it. Now I see a different problem. I want to change the status the menu depending on some condition in my custom view display, for example, change th etext, activate or deactivate some menus. Here si what I am doing:
    I added a menu item in my mainmenu.nib "expand"
    added a action in firsresponder and made the connection
    added an action method in my customview class named "expand"
    So far so good, now I added the following:
    in my customview class, I added:
    SEL action = {menuitem action];
    if (action==@expand){
    // do something;
    return YES;
    But the problem is now is that when I select the expand menu, the action here reports the following: (in debugger)
    SEL action variable <null selector>

    So I do not get the "expand" item to compare !!!!
    What is happening and why am I getting this <null selector>? how do I fix it?

Share This Page