IOS 11 Development Tutorial (22) IOS11 Application View Implementation button Response (2)

Source: Internet
Author: User
Tags set background

IOS 11 Development Tutorial (22) IOS11 Application View Implementation button Response (2)

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.white// Set Background color
  • Isyellow=false
  • }else{
  • Self.view.backgroundcolor=uicolor.yellow
  • 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

IOS 11 Development Tutorial (22) IOS11 Application View Implementation button Response (2)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.