Swift3 - addTarget不起作用

时间:2017-03-25 18:47:00

标签: ios swift uiview addtarget

    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)
    }
}

我想管理MyClass中的事件,但不调用选择器。 如果我在UIViewController中执行相同的逻辑,它可以工作,但在这种情况下不行。谁能帮我?感谢

0 个答案:

没有答案