gpt4 book ai didi

ios - 如何将事件发送到我的(子)UIView?

转载 作者:行者123 更新时间:2023-11-28 15:38:00 26 4
gpt4 key购买 nike

我是 Swift 的新手,很难理解事件流。下面的代码可以直接在 xcode playground 中运行。我在后台有一个白色的 UIView。该 View 有一个棕色按钮和一个红色 View 作为 subview 。单击它们,事件将按预期记录在 Controller 中。

但是这个白色 View 的 Controller 还添加了另一个 View ,它有自己的 Controller 类 (SubviewController)。 SubviewController 是绿色的,有一个带有黑色按钮的蓝色 subview 。问题是……为什么我没有从绿色、蓝色和黑色 View /按钮中获取任何日志?

import Foundation
import UIKit
import PlaygroundSupport

class TestViewController : UIViewController {

let playButton: UIButton = {
let playButton = UIButton(frame: CGRect(x: 155, y: 135, width: 160, height: 40))
playButton.setTitle("BROWN BUTTON", for: .normal)
playButton.backgroundColor = UIColor.brown
return playButton
}()


override func loadView() {
let viewWhite = UIView()
viewWhite.backgroundColor = UIColor.white

let viewRed = UIView()
viewRed.backgroundColor = UIColor.red
viewRed.frame = CGRect(x: 20, y: 20, width: 40, height: 10)
viewRed.clipsToBounds = true
let recognizer2 = UITapGestureRecognizer(target: self, action: #selector (self.handleTapRed(_:)))
viewRed.addGestureRecognizer(recognizer2)


let recognizer = UITapGestureRecognizer(target: self, action: #selector (self.handleTap(_:)))

viewWhite.addGestureRecognizer(recognizer)

playButton.addTarget(self, action: #selector (self.action) , for: .touchUpInside)

let catList = SubviewController()

viewWhite.addSubview(catList.view)
viewWhite.addSubview(playButton)
viewWhite.addSubview(viewRed)
self.view = viewWhite
}

func action() {
print("Brown button tapped")
}

func handleTap(_ sender:UITapGestureRecognizer){
print("WHITE VIEW (background view) TAPPED")
}

func handleTapRed(_ sender:UITapGestureRecognizer){
print("RED VIEW TAPPED")
}

}

class SubviewController: UIViewController {

let buttonBlack: UIButton = {
let button = UIButton(frame: CGRect(x: 40, y: 10, width: 170, height: 20))
button.backgroundColor = UIColor.black
button.setTitle("BLACK BUTTON", for: .normal)
return button
}()

let viewBlue: UIView = {
let v = UIView()
v.backgroundColor = UIColor.blue
v.frame = CGRect(x: 20, y: 40, width: 240, height: 60)
v.clipsToBounds = true
return v
}()

override func loadView() {
super.loadView()
self.view.backgroundColor = UIColor.green
buttonBlack.addTarget(self, action: #selector (self.blackKlick) , for: .touchUpInside)

self.view.addSubview(viewBlue)

self.view.frame = CGRect(x: 0, y: 40, width: 240, height: 60)
self.view.clipsToBounds = true

self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector (self.handleTapGreen(_:))))

viewBlue.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector (self.handleTapBlue(_:))))
viewBlue.addSubview(buttonBlack)
}

func blackKlick() {
print("Black button tapped")
}

func handleTapBlue(_ sender:UITapGestureRecognizer){
print("BLUE VIEW TAPPED")
}

func handleTapGreen(_ sender:UITapGestureRecognizer){
print("GREEN VIEW TAPPED")
}
}

PlaygroundPage.current.liveView = TestViewController()

感谢您的帮助!

最佳答案

当前代码中的这一行:

    let catList = SubviewController()

创建SubvieController本地 实例。一旦退出 loadView() 函数,该实例就会消失。

因此,您需要一个类级变量来保存该实例。添加这一行:

class TestViewController : UIViewController {

var catList: SubviewController!

然后从 loadView() 的实例化行中删除 let:

    catList = SubviewController()

关于ios - 如何将事件发送到我的(子)UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44087210/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com