Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement the response of buttons in the iOS11 application view

Shulou Source: shulou.com Published: 2022-06-02 05:49:27 10月04日 Update

This article mainly introduces iOS11 application view how to achieve button response, the article is very detailed, has a certain reference value, interested friends must read!

2. Responses implemented using the Code Add button

Using the button added by the code, the response needs to use the addTarget(_:action:for:) method, which has the following syntax:

func addTarget(_ target: AnyObject?, action: Selector, for controlEvents: UIControlEvents)

The parameters are described as follows:

Target: Indicates the target object. It is the sender of the action message.

action: Represents a selector used to identify an action message. It cannot be empty.

controlEvents: Represents control events. There are 19 control events in iOS, as shown in Table 2-4.

Table 2-4 Control Events

touchDown

touchDownRepeat

1: When the user presses the second, third, or fourth finger.

touchDragInside

touchDragOutside

touchDragEnter

touchDragExit

touchUpInside

touchUpOutside

(Touch must start inside the control to send notifications).

touchCancel

valueChanged

editingDidBegin

editingChanged

editingDidEnd

editingDidEndOnExit

allTouchEvents

allEditingEvents

applicationReserved

aystemReserved

AllEvents

[Example 2-5] The following will realize the function of tapping the button to change the background color of the main view. The code is as follows:

import UIKit

class ViewController: UIViewController {

var isCyan:Bool=false

override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

let button=UIButton(frame: CGRect(x: 90, y: 545, width: 225, height: 30))

button.setTitle("Tap me,Change View Color", for: UIControlState())

button.setTitleColor (UIColor.black, for: UIControlState()) //Set the color of the button title

self.view.addSubview(button)

button.addTarget(self, action: #selector(ViewController.tapbutton), for: UIControlEvents.touchUpInside)

}

@objc func tapbutton(){

if(isCyan){

self.view.backgroundColor=UIColor.white

isCyan=false

}else{

self.view.backgroundColor=UIColor.cyan

isCyan=true

}

}

……

}

Run the program at this point, and you will first see the effect shown in Figure 2.14. When you tap Tap me,Change View Color, the background of the main view changes to cyan, as shown in Figure 2.15. When you tap Tap me,Change View Color again, the background color of the main view will change back to white.

Figure 2.14 Operation Effect Figure 2.15 Operation Effect

The above is "iOS11 application view how to achieve button response" all the content of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

Tags: Button control home view event code effect background run view application content action title message article color such as figure value interest function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Tech Info Shulou Technology Microsoft Apple