iOS series basic article action and output
Directory:
1. Preface and Case description
2. What is an action?
3. What is Export?
4. Actual combat
5. End
1. Preface and Case description
We learned about tags and buttons in the previous section, and below we will further practice the use of these two controls using a project case with user interaction, as well as control of movement and output.
This case is based on the interface design of the previous content, the conceptual flow of the function
The action and export mechanism of this case
2. What is an action?
An action is a method defined in response to an event of a control, similar to a. NET WinForm to add an action method to a control event:
1 Private void OnClick (object sender, EventArgs e) {2 // ... 3 }
However, in iOS, the return value type of the action code is ibaction and the sample code is as follows:
1 @IBAction func OnClick (sender:anyobject) {2 // ... 3 }
The return type of the method is Ibaction, which indicates that this method is an action.
Same as action method in. NET WinForm: Sender is a parameter, an event source, a control object that emits the current event, or it can be omitted as follows:
1 @IBAction func OnClick () {2 // ... 3 }
3. What is Export?
Attention:
"Export" this thing called very wonderful, maybe some friends do not understand, I will from. NET and Android two examples (no other I would not.) )。
You can understand the best, do not understand the words do not have to delve into these two examples, directly in the blink of an eye to see iOS on the line.
In the design interface of the. NET WinForm, we drag and drop a control, and the VS will automatically be in the post code of the current form (XXX. Designer.cs) generates a response in the control creation code.
This allows us to use the This . Control name in the predecessor code file to access the control variables in the post code, thereby manipulating the controls on the form.
In Android, after we have placed the control on the design interface, if we need to access the control in a code file, for example:
1 button btn = (button) Findviewbyid (r.id.btnxxx);
This allows the control of the ID btnxxx on the interface to be controlled in the function code through the object btn.
From these two examples, you should be able to guess, "export" in iOS is actually referring to the above control in the function code of the object.
In iOS, in order for an event of a control to be associated with a defined action, we can associate it with Interface Builder or code, and in this article we will focus on the first way.
As with the action, we also need to establish a connection to the export and control, which can also be done by Interface Builder or code.
Tips:
The Interface Builder Designer is Interface Builder, which is integrated into the Xcode tool after Xcode 4.
Open the storyboard storyboard or xib file, and the Interface Builder designer opens automatically.
4. Actual combat
In order to achieve this objective case, so that the button can control the label, we need to define the label and connect the output to the button to achieve the action.
Click on the "Open auxiliary Editor" button in the upper right corner to open the interface so that you can write the function code at the same time as the design interface:
With the tag label selected, hold down the control key and drag it to the right Auxiliary editor window to release the mouse:
When you release the mouse, a dialog box pops up, select outlet in the connection bar, and set the output name to Label1,type type Uilabel:
By clicking the Connect button, you will find that the following code is automatically added to the auxiliary editor on the right:
In the same way, hold down the control key and drag the button to the Secondary editor window, set as follows:
Click the Connect button and you'll find an action method vividly added to the auxiliary editor on the right:
We implement the following code in the action method onclick:
This way, when we click the button, we respond to the Execute OnClick action method, which modifies the text content of its bound label through the Label1 object.
The results of the case run as follows:
5. Summary
Although the application is not how tall, but after all, we have made the first case. It's still pretty exciting.
"Perfectly formed", we refuel together, let this sparrow fly more proud 、??
iOS series basic article action and output