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

Posted

技术标签:

【中文标题】如何将事件发送到我的(子)UIView?【英文标题】:How can I get events to my (sub) UIView? 【发布时间】:2017-05-20 14:44:06 【问题描述】:

我是 Swift 新手,很难理解事件流。下面的代码可以直接在 xcode 游乐场中运行。我在后台有一个白色的 UIView。这个视图有一个棕色按钮和一个红色视图作为子视图。单击它们,事件将按预期记录在控制器中。

但是这个白色视图的控制器还添加了另一个视图,它有自己的控制器类(SubviewController)。 SubviewController 是绿色的,并且有一个带有黑色按钮的蓝色子视图。问题是......为什么我没有从绿色、蓝色和黑色视图/按钮中获得任何日志?

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()

感谢您的帮助!

【问题讨论】:

【参考方案1】:

当前代码中的这一行:

    let catList = SubviewController()

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

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

class TestViewController : UIViewController 

    var catList: SubviewController!

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

    catList = SubviewController()

【讨论】:

天哪,多么愚蠢... 2 小时没看到... 非常感谢!

以上是关于如何将事件发送到我的(子)UIView?的主要内容,如果未能解决你的问题,请参考以下文章

如何正确使用 Angular 中的 Observable 将数组数据从父级发送到子级?

如何将数组发送到子视图然后打印到标签中?

UIView 事件转发无限循环

无法将子视图添加到我的 UIView

将触摸事件从 uiview 传输到 uiwebview

如何在需要时(而不是定期)将服务器发送的事件通知发送到特定的浏览器会话?