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

标签 ios iphone swift uiview

我是 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/

相关文章:

ios - 使用 contains 检查 NSManagedObject Array 是否具有特定键的值

iphone - iOS 启动画面淡入淡出缩小之物

iphone - 如何在 iPhone 的锁定屏幕上开始播放音乐

swift - 何时在 UIKit 生命周期中使用 SwiftUI 接口(interface)?

ios - 点击手势识别器干扰清除按钮

objective-c - NSURLConnection 后查看 Controller Segue 延迟

ios - 如何解决设备之间的 CloudKit 功能不一致

iphone - 如何为 iOS 应用程序设置 UILabel 的左上角对齐?

ios - 如何解决 NSURLError : The network connection was lost?

ios - 如何从字典中填充 UIPickeView?