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

john903

macrumors member
Original poster
Apr 11, 2008
66
0
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.
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
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.
 

MrFusion

macrumors 6502a
Jun 8, 2005
613
0
West-Europe
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.

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.
 

john903

macrumors member
Original poster
Apr 11, 2008
66
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.