Creating an Action Connection
Create an action connection to send a message from a control to your code. When the user clicks a button control, for example, the button should send an action message telling your code to execute an appropriate action.
Select a storyboard or xib file in the project navigator.
The file’s contents open in Interface Builder.
In Interface Builder, select the control you want to configure by clicking it in the outline view or on the canvas.
A control is a user interface object—such as a button, text field, switch, scroll bar, or slider—that causes instant actions or visible results when the user manipulates the object. If the outline view doesn’t appear, display it by clicking the Show Document Outline () control in the lower-left corner of the canvas. Select a control from the list of nested objects.
Choose View > Assistant Editor > Show Assistant Editor.
The assistant editor opens your object’s implementation file.
Control-drag from the control in Interface Builder to the implementation file.
Xcode indicates where you can insert an action method in your code. (In the screenshot, the assistant editor displays the implementation file of the view controller for the Warrior button.)
Release the Control-drag.
The assistant editor displays a Connection menu.
Enter the name of the action method, and click Connect.
In the implementation file, Xcode inserts a skeletal definition for the new method, as shown below. The
IBActionreturn type is a special keyword indicating that the instance method can be connected to your storyboard or xib file. Xcode also sets the action selector for the control to this method. As a result, the method gets invoked whenever the control receives an action message.
Add to the skeletal definition the source code that implements the action method.
Whereas activation of the control is implemented by the system, you must implement the control’s behavior. In the screenshot below, the action method starts a new game when the user clicks the Warrior button.