iOS - Actions and Outlets
Introduction
Actions and outlets in iOS are referred to as ibActions and ibOutlets respectively where ib stands for interface builder. These are related to the UI elements and we will explore them after knowing visually how to implement them.Steps Involved
1. Let's use our First iPhone Application.2. Select the ViewController.xib file from the files in the navigator section.
3. Now you can select the UI elements from the library pane in right hand side of our window which is shown below.
4. You can drag and drop UI elements to our view in our interface builder.
5. Let add a Label and Round Rect Button to our view.
6. From the Editor selector button in the workspace toolbar found on the top right corner as shown below.
Select Assistant editor button
7. We will see two windows in our editor area in the center, one is ViewController.xib file and other is ViewController.h
8. Now right click on the label and select, hold and drag new referencing outlet as shown below
9. Now drop in the ViewController.h in between the curly braces. There may be no curly brace please in the file, if so add before doing this. You will find a pop up as shown below.
10. Now type the label name for the outlet, here I have given myTitleLabel. Click connect and the ibOutlet will be complete.
11. Similarly to add an action right click the Round rect button, select touch up inside and drag it below the curly braces
12. Drop it and name it setTitleLabel.
13. Now select ViewController.m file, you'll find a method as shown below.
-(IBAction) setTitleLabel:(id)sender{
}
14. Add a statement as shown below inside the above method.[myTitleLabel setText:@"Hello"];
15. Now let's run the program by selecting the run button. You will see the following output.16. Now click the button.
17. The label that we created outlet have been changed by the action on the button.
18. So from the above example we can conclude that IBOutlet creates a reference to the UIElement (here for the UILabel) and similarly the IBAction links the UIButton with a method which is called on the event touch up inside.
19. You can play around with actions by selecting different events while creating the action.
0 comments:
Post a Comment