简体   繁体   中英

AddTarget connected to UIButton in customView does not work. (swift5, iOS)

There is a custom UIView with UIButton. The viewController attempts to connect the function to that UIButton through addTarget(...). But it doesn't work.

class customView: UIView {
     var button: UIButton = {
        //some properties
     }()
}
class viewController: UIViewController {
     var cv = customView()
     override func viewDidLoad() {
         cv.button.addTarget(self, action: #selector(someFunction), for: .touchUpInside)
     }
     @objc func someFunction() {
          print("Some function!")
     }
}

This code is not the one I am writing. But it's a very brief representation of the code I'm writing. SomeFunction does not work when the button is touched. What more is needed?

It is difficult to find out the actual problem on reduced example code, that is not running on itself. The use of addTarget itself is fine, so I suppose the problem is somewhere in the code, that we do not see here.

One educated guess would be a problem with the size/layout of your custom view. Depending on the view, it might happen that the bounds of the view are smaller than the button or zero, although you will still see the full button, but you cannot click it. You might notice this on a standard button if you do not get any color effects on the button while clicking it.

Another good next step would be to have a look into Xcode's View Debugger to see if there is anything wrong with the view sizes.

For reference, I gave your sample code a little edit to get it running in a Playground and your function is just triggered fine there.

import PlaygroundSupport
import UIKit

class CustomView: UIView {
    var button = UIButton(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 20.0))

    override init(frame: CGRect) {
        super.init(frame: frame)

        button.backgroundColor = .blue
        button.setTitle("click me", for: .normal)
        addSubview(button)
        button.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        button.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class ViewController: UIViewController {
    var customView = CustomView()

    override func viewDidLoad() {
        customView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(customView)

        customView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        customView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

        customView.button.addTarget(self, action: #selector(someFunction), for: .touchUpInside)
    }

    @objc func someFunction() {
        print("Some function!")
    }
}

PlaygroundPage.current.liveView = ViewController()

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