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.

bullet
To create an action connection
  1. Select a storyboard or xib file in the project navigator.

    The file’s contents open in Interface Builder.

  2. 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 (../art/DockViewControl_2x.png) control in the lower-left corner of the canvas. Select a control from the list of nested objects.

  3. Choose View > Assistant Editor > Show Assistant Editor.

    The assistant editor opens your object’s implementation file.

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

    ../art/insert_action_2x.png
  5. Release the Control-drag.

    The assistant editor displays a Connection menu.

  6. Enter the name of the action method, and click Connect.

    ../art/name_the_action_method_2x.png

    In the implementation file, Xcode inserts a skeletal definition for the new method, as shown below. The IBAction return 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.

    ../art/skeletal_action_method_2x.png
  7. 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.

    ../art/implement_action_method_2x.png

Related Articles