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.
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.
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.