How to Add an Action in Your iOS App - dummies

How to Add an Action in Your iOS App

By Jesse Feiler

After you have the TestDriveController interface displayed for your iOS app, either by having the Assistant editor display it automatically or by navigating to it using the Jump bar, the actual business of creating an action is pretty straightforward. From within the Interface Builder editor, just Control-drag from the element you’re interested in (the Test Drive button, in this case) to the TestDriveController interface, as detailed in the following steps:

  1. In the Project navigator, select the Main_iPad.storyboard file.

  2. Close the Utility area by deselecting it in the View selector.

    You don’t need it to create the action.

  3. Open the Assistant editor by clicking its button in the Editor selector in the toolbar.

    You should see the TestDriveController.m interface file displayed in the Assistant editor.

  4. Control-click and drag from the Test Drive button in the view (the car image, in this example) to the TestDriveController.m file, right between the @interface and –d statements.

  5. In the dialog that appears, choose Action from the Connection drop-down menu.

    image0.jpg

  6. In the same dialog, leave Touch Up Inside as the selection in the Event drop-down menu.

    To create an Action for a control, you need to specify what event triggers the action. In the case of a button, a Touch Up Inside event is the usual choice because Touch Up Inside is the event that’s generated when the very last place the user touched before lifting her finger is still inside the button.

  7. Still in the same dialog, name this action testDrive by entering testDrive in the Name field; then click Connect.

    A new action method

    - (IBAction)testDrive:(id)sender;

    gets added to your code.

    image1.jpg

Xcode also adds code for you in the .m file to support whatever it is that you’re doing. (In this case, the added code supports the action.)

image2.jpg

You can always double-check the status of your Target-Action connections with the help of the Connections inspector. With the Utility area displayed, click the Connections inspector icon in the Inspector selector bar to open the Connections inspector or right-click the view controller in the Document Outline to get a similar picture in the Connections window.

In the Received Actions section, you’ll see the new action. You can change the Interface Builder object you are receiving the action from by dragging from the circle to the new Interface Builder object. You can also add an action by entering the code the Interface Builder would have added for you (the method declaration and definition) in view controller’s @interface.

- (IBAction)testDrive:(id)sender;

And @implementation

- (IBAction)testDrive:(id)sender {}

and then dragging from the circle in the Received Actions section in the Connections inspector or window to the control and choose an Event type from the contextual menu that appears when you release the mouse button.