Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将按钮视图作为子视图添加到标签视图时,手势识别器不工作_Ios_Swift - Fatal编程技术网

Ios 将按钮视图作为子视图添加到标签视图时,手势识别器不工作

Ios 将按钮视图作为子视图添加到标签视图时,手势识别器不工作,ios,swift,Ios,Swift,我使用一个堆栈视图来渲染其中的标签,然后将按钮添加到相应的标签中,在点击按钮时使用一个目标来处理事件。在这里,当作为子视图添加到标签时,不会调用处理程序 Stackview label1 button1 label2 button2 label3 button3 将按钮添加到labelview的功能 func setButton(count:Int){ for i in 0..<count{ buttons[i]

我使用一个堆栈视图来渲染其中的标签,然后将按钮添加到相应的标签中,在点击按钮时使用一个目标来处理事件。在这里,当作为子视图添加到标签时,不会调用处理程序

Stackview

label1 
      button1

label2 
      button2

label3
      button3
将按钮添加到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])

    }

将标签userInteraction设置为true


另一个更好的解决方案是在标签外添加按钮,这样标签和按钮都位于容器视图中。

很高兴它起到了作用!
@objc func handleDelete(_ sender:UIButton){

    print(sender.tag)
}