2
    class MyClass {

    let customView: MyView

    init() {
        customView = MyView()
        customView.checkBox.addTarget(self, action: #selector(checkBoxValueChanged(_:)), for: .valueChanged)
    }

    @objc func checkBoxValueChanged(_ sender: M13Checkbox!) {
        print("checkBoxValueChanged")
    }

}

@IBDesignable class MyView: UIView {

    @IBOutlet var view: UIView!
    @IBOutlet var checkBox: M13Checkbox!

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

        setupView()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        setupView()
    }

    func setupView() {
        let bundle = Bundle(for: type(of: self))
        UINib(nibName: Constant.xib.MY_XIB, bundle: bundle).instantiate(withOwner: self, options: nil)

        view.frame = bounds
        view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        addSubview(view)
    }
}

I want to manage an event in MyClass but the selector is not called. If I do the same logic in UIViewController it works, but not in this case. Can anyone help me? Thanks

Louis
  • 406
  • 6
  • 13
  • Are you calling the corresponding `sendActions(for: valueChanged)` in your `MyView` class? – Paulo Mattos Mar 25 '17 at 18:58
  • Hi @PauloMattos Where do you want call sendAction? MyView is not a UIControl. But M13CheckBox call sendAction – Louis Mar 25 '17 at 19:08
  • 1
    Ok, looks like it should work then. Another hint: are you holding a **strong reference** to the corresponding `MyClass` instance? (From `addTarget` documentation: *The control does not retain the object in the target parameter. It is your responsibility to maintain a strong reference to the target object while it is attached to a control.*) – Paulo Mattos Mar 25 '17 at 19:12
  • @PauloMattos Yes, class MainViewController: UIViewController { var myclass: MyClass?; ... – Louis Mar 25 '17 at 19:17
  • @PauloMattos Now it's work, i fail my strong reference... Thanks for your help – Louis Mar 25 '17 at 19:27

0 Answers0