简体   繁体   中英

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

 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.

Another better solution is to add the button outside the label, so both the label and button are in a container view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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