Your object receives messages from a control if it is the target of an action defined by the control. For example, when the user clicks a button, the button sends an action message to a target. When you add an action connection, Interface Builder adds an action method to your class and sets the target for the control to an instance of your class. Then you are responsible for implementing the action method.
In Interface Builder, open the assistant editor (choose View > Assistant Editor > Show Assistant Editor).
Use the jump bar at the top of the assistant editor to select the implementation file of the object that will receive the action message.
Control-drag from the control, in the outline view or in the canvas, to the code in the assistant editor.
Xcode indicates where you can insert an action method in your code, or if you hover over a method, Xcode indicates that it can be the action for the target.
In the popover that appears, choose Action from the Connection menu, enter the name of the action method, and choose an event from the Event pop-up menu.
Click Connect.
In the implementation file, Xcode inserts a code snippet for the action method. The IBAction
return type is a special keyword indicating that the instance method can be connected to your user interface file. Xcode sets the target of the control to an instance of your class (for example, a view controller object), and sets the action of the control to the action method selector. At runtime, when the specified event occurs, the action message is set to your object.
Implement the action method.
It’s your responsibility to write the code for the action method.