add actions to First Responder

Discussion in 'Mac Programming' started by john903, May 3, 2008.

  1. john903 macrumors member

    Joined:
    Apr 11, 2008
    #1
    How do you add your own custom actions in the First Responder in Interface Builder? I'm working on my first Cocoa app and I don't understand how to create my own actions for my custom menu items.
     
  2. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #2
    Normally you would create at least one controller object (the "application controller", just an NSObject subclass) where you could write methods that get called by menu items. Then in Interface Builder, you instantiate the controller and control-drag from the menu item to your controller object and select the target method. You could also have more controller objects that manage specific parts of your application.

    I think if you download some of the simple Cocoa applications and examine how they are constructed (including looking at all the connections and objects in IB), it will help you. That said, the process is not exactly the same for every developer or every application. There is often more than one way to go about it.
     
  3. MrFusion macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #3
    Or, create a new cocoa Application project and go to your mainMenu nib. Click on First responder, go to Classes view and an action (You can only add actions to first responder) e.g. testButton.
    Don't connect anything to something (control-drag)

    Then go to your MyDocument.h in xcode and add:
    Code:
    (IBAction) testButton:(id)sender {
    NSLog(@"click");
    }
    
    And it works, it's magic. I once asked this same question and got a response from a cocoa guru explaining how it works. If you want to know how it works, you could try looking for that thread.
    What method you should use, depends - I think - on how your program is designed. My explanation is the only way to do it for separate Menu nib's, I think. While the method from the other guy works well when the actions and the code are together in one logic unit described by a nib. That is, you can't control-drag across nib's.
     
  4. john903 thread starter macrumors member

    Joined:
    Apr 11, 2008
    #4
    Thanks for your help! I actually discovered a really simple way while looking into the solutions you guys provided.

    It's really simple. Just click First Responder and then click the Identity tab in Inspector. It gives you a list of all the class actions and allows you to create your own using + and - buttons below the list. Worked perfect.
     

Share This Page