Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 230

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report