Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect a button to a method on Mac OS X

I am used to programming for the iPhone. There, I would connect a button to an action, and then a method by creating the method like so: -(IBAction) DoStuff{…}. Then I would make an outlet for the button, and then the actual button in Interface Builder. I would then connect the button to the outlet, and then connect the button to the action by clicking on the circle next to Touch Up Inside and drag it over to File's Owner and select my action.

I am new to programming for the Mac, so I tried to drag from performClick to the file I wanted, but it wouldn't let me make the connection. Do I have to do this programmatically or what? How do I get this button to trigger an action in my code?

like image 597
Regan Avatar asked Sep 24 '10 02:09

Regan


3 Answers

The fundamental difference is that iOS controls can have multiple actions for different events, but Mac OS X controls only have one primary action (in some cases, there are others that can be set up programatically).

When you right-click on a button in a Mac nib, performClick: is under Received Actions; it’s not an event. The only entry under Sent Actions is “selector”, which is the only thing you can connect to an action on another object.

Because there is only one “sent event”, you’ll normally just control-drag/right-drag from the control to the target and select the action rather than control-clicking, selecting the event and dragging from that.

like image 106
Jens Ayton Avatar answered Nov 02 '22 14:11

Jens Ayton


It works much the same, but unlike UIKit there is only one signature for actions:

- (IBAction)actionName:(id)sender;

See Communicating with Objects and Target/Action in Interface Builder for more.

like image 34
Georg Fritzsche Avatar answered Nov 02 '22 12:11

Georg Fritzsche


I like to Control-Click on the button and then drag to the object that I want to recieve the action. I then select the method of possible choices from the popup menu.

like image 1
No one in particular Avatar answered Nov 02 '22 12:11

No one in particular