Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

iOS Actions (action) and Outlets (Outlet)

Introduction

In iOS, actions (action) and outlets (Outlet) refer to ibActions and ibOutlets, which are where the ib Interface Builder is. These are all related to UI elements; we will understand them intuitively and then discuss how to implement them.

Step

1. Let's use the first iPhone application.

2. Select the ViewController.xib file from the files in the navigation section

3. Select UI elements from the window grid library displayed below the window on the right-hand side

4. Drag UI elements into the visual frame of Interface Builder

5. Add a label and a red rounded button to the visual view

interfaceAction

6. Find the editor selection button in the upper-right corner of the workspace toolbar, as shown below

StandardEditor

Select the Editor Button

AssistantEditor

7. There are two windows in the center of the editor area, the ViewController.xib file and ViewController.h

8. Right-click the selection button on the label, hold and drag the new reference outlet, as shown below

ibOutletDrag

9. Now place it between the braces in ViewController.h. It can also be placed in the file; if so, it must have been added before doing this. As shown below

ibOutletDrop

  1. For the outlet (Outlet) label name, myTitleLable is given here. Click the link to complete the ibOutlet

11. Similarly, to add an action, just right-click the rounded rectangle, select Touch Up Inside, and drag it below the braces

ActionDrag

12. Rename it to setTitleLable

ActionDrop

13. Select the ViewController.m file; there is a method as follows

-(IBAction) setTitleLabel:(id)sender{

}

14. Inside the above method, add a statement as follows

[myTitleLabel setTitleText:@"Hello"];

15. Select the Run button to run the program and get the following output

IBActionTutorial.Simulator_Start

16. Click the Button

IBActionTutorial.Simulator_end

17. The created outlets button label has changed to an action (actions) performed on the button

18. From the above, we know that IBOutlet creates a reference to a UIElement (here UILable), and similarly IBAction links to UIButton through the action performed on UIButton.

19. When creating an action, you can perform different operations by selecting different events.