Implementation of button response IOS9 Implementation of button response in IOS 9 app development tutorial iOS9
The main button is to implement the user interaction, that is, to implement the response. The way buttons implement responses can be divided into two different types of buttons: one is the response of the edit interface to add a button implementation, and the other is the response that is implemented using the Code add button.
1. Edit Interface Add button implementation response
Using the Edit interface to add buttons allows you to use drag to implement a button response, which is also the simplest way to implement a response.
"Example 2-4" The following will implement the Tap button to change the function of the main view background color. The following steps are described:
(1) Create a single View application template type project, named Uibutton-response.
(2) Open the Main.storyboard file and set the size of the main view to iphone 4.7-inch. Drag a button control from the view library into the main master to set the title to tap Me,change View Color.
(3) Use the icon in the three view mode of the settings editor to adjust the Xcode interface to the effect shown in 2.5. This process is explained in the previous chapters.
Figure 2.5 Tuning the Xcode interface
(4) Holding down the CTRL key to drag the button object in the interface, a blue line will appear, drag the Blue Line to the blank of the Viewcontroller.swift file, as shown in 2.6.
Figure 2.6 Ctrl + Drag the button object in the interface
(5) When you release the mouse, the dialog box that declares the associated socket variable will pop up (explained in the previous section), as shown in 2.7.
Figure 2.7 Popup declaration associated with a Socket variable dialog box
(6) Set the connection option to action, which indicates that an action is associated, and the name is set to Tapbutton, indicating that the associated action name is tapbutton,2.8.
Figure 2.8 Filling out a dialog box
Note: The name here can be arbitrary.
(7) Click the Connect button to see the code shown in the Viewcontroller.swift file in 2.9.
Figure 2.9 Action
At this point, when the user taps the button, a method called Tapbutton () is triggered.
Note: This is done in conjunction with an action declaration and an association, and a way to first declare an action-related association. Declaring an action can use the keyword ibaction. The keyword can tell the interface of the story panel that this method is an action and can be triggered by a control. The syntax for declaring an action is as follows:
- @IBAction func action name (parameter: parameter type) {
- }
2.10, which is the declaration code of the action written in the Viewcontroller.swift file.
Figure 2.10 The actions declared
Note: After the action is declared, a small, hollow circle appears in front of the code, which indicates that the action has not been associated.
Once you've declared your actions, you can associate them, using the tools in the adjustment window to tweak the Xcode interface. Adjust it to the same effect as in Figure 2.5.
Then, holding down the CTRL key while dragging the button object in the interface, a blue line will appear, associating the Blue line with the action in the file Viewcontroller.swift, as shown in 2.11.
Figure 2.11 Associated actions
Finally, after releasing the mouse, the button object is associated with the success of the action, when the small white circle in front of the action becomes a small, solid circle, which indicates that the action has been associated with the same effect as in Figure 2.9.
(8) Open the Viewcontroller.swift file and write the code that will implement the button's response. The code is as follows:
- Import UIKit
- Class Viewcontroller:uiviewcontroller {
- var isyellow:bool=false
- @IBAction func Tapbutton (sender:anyobject) {
- Determines whether the background of the main view is yellow
- if (Isyellow) {
- Self.view.backgroundcolor=uicolor.whitecolor () //Set the background color of the main view
- Isyellow=false
- }else{
- Self.view.backgroundcolor=uicolor.yellowcolor ()
- Isyellow=true
- }
- }
- ......
- }
Running the program at this point will first see a 2.12 effect. When the tap Me,change View color button is tapped, the background of the main display turns yellow, as shown in 2.13. When you tap the tap me,change View Color button again, the background color of the master will change back to the original white.
Figure 2.12 Run effect Figure 2.13 Run effect
2. Using code to add a button implementation response
Using the buttons added by the code, the implementation response needs to use the Addtarget (_:action:forcontrolevents:) method, which has the following syntax:
- Func AddTarget (_ Target:anyobject?,
- Action Action:selector,
- forControlEvents controlevents:uicontrolevents)
Among them, the parameters are described as follows:
- Target: Represents the destination object. It is the sender of the action message.
- Action: Represents the selector, which is used to identify the action message. It cannot be considered empty.
- ControlEvents: Represents a control event. There are 19 kinds of control events in iOS, as shown in table 2-4.
Table 2-4 Control Events
"Example 2-5" The following will implement the Tap button to change the function of the main view background color. The code is as follows:
- Import UIKit
- Class Viewcontroller:uiviewcontroller {
- var iscyan:bool=false
- Override Func Viewdidload () {
- Super.viewdidload ()
- Additional setup after loading the view, typically from a nib.
- Add a Button object
- Let Button=uibutton (Frame:cgrectmake (90, 545, 225, 30))
- Button.settitle ("Tap Me,change View Color", forState:UIControlState.Normal)//Set the caption of the button
- Button.settitlecolor (Uicolor.blackcolor (), forState:UIControlState.Normal)//Set the color of the button caption
- Self.view.addSubview (Button)
- Implementing a button response
- Button.addtarget (Self, Action: "Tapbutton", ForControlEvents:UIControlEvents.TouchUpInside)
- }
- Func Tapbutton () {
- Determines whether the background color of the main view is cyan
- if (Iscyan) {
- Self.view.backgroundcolor=uicolor.whitecolor ()
- Iscyan=false
- }else{
- Self.view.backgroundcolor=uicolor.cyancolor ()
- Iscyan=true
- }
- }
- ......
- }
Running the program at this point will first see a 2.14 effect. When the tap Me,change View color button is tapped, the background of the master is changed to cyan, as shown in 2.15. When you tap the tap me,change View Color button again, the background color of the master will change back to the original white.
Figure 2.14 Run effect figure 2.15 Run effect
This article is selected from:IOS 9 Application Development Basic Tutorial University bully internal information, reproduced please indicate the source, respect the technology respect the IT person!
Implementation of button response in IOS 9 app development tutorial iOS9