简体   繁体   中英

Swift - How to set a click event on a UIStackview

I am trying to set a click event on my UIStackview to open a viewcontroller. When I do this nothing happens. It doesn't even print the line when I tap it. I tried multiple solutions on this website, like UITapGestureRecognizer but to no avail. What am I doing wrong?

My code:

private let stackUIView: UIView = {
     let stackUIView = UIView(frame: CGRect(x: 25, y: 25, width: 400, height: 90))
    let tap = UIGestureRecognizer(target: self, action: #selector(stackviewClicked))
    stackUIView.addGestureRecognizer(tap)
    stackUIView.isUserInteractionEnabled = true
    
    return stackUIView
}()


@objc
    func stackviewClicked() {
        print("UIView Clicked")
        let testVC = testViewController()
        navigationController?.pushViewController(testVC, animated: true)

    }

It should be UITapGestureRecognizer not UIGestureRecognizer

let tap = UITapGestureRecognizer(target: self, action: #selector(stackviewClicked))

For me works, thanks

termsAndConditionsStack.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(stackviewClicked)))

@objc func stackviewClicked() {
     print("works")
}

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