简体   繁体   English

将按钮视图添加到标签视图作为子视图时,手势识别器无法正常工作

[英]Gesture recogniser not working when added button view to the Label view as subview

I Used a stack view to render labels in it and then added the buttons to the respective labels, used a target to handle the event when button is tapped. 我使用堆栈视图在其中呈现标签,然后将按钮添加到相应的标签,使用目标来处理按钮被点击时的事件。 Here the handler is not called when added as subview to the label 这里,当作为子视图添加到标签时,不会调用处理程序

Stackview

label1 
      button1

label2 
      button2

label3
      button3

Function to add button to the labelview 用于向labelview添加按钮的功能

 func setButton(count:Int){

    for i in 0..<count{

        buttons[i].tag = i

        buttons[i].addTarget(self, action: #selector(handleDelete(_:)), for: .touchUpInside)

        label[i].addSubview(buttons[i])

    }

Handler 处理器

@objc func handleDelete(_ sender:UIButton){

    print(sender.tag)
}

Set the labels userInteraction to true. 将标签userInteraction设置为true。

Another better solution is to add the button outside the label, so both the label and button are in a container view. 另一个更好的解决方案是在标签外添加按钮,因此标签和按钮都在容器视图中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM