3

我想要一个协议:

protocol CameraButtonDelegate: class {
    func cameraButtonDidPress(_ sender: UIButton)
}

因此,我可以将任何客户端分配给一个按钮,例如:

cameraButton.addTarget(delegate, action: #selector(cameraButtonDidPress), for: .touchUpInside)

但是,它无法编译,因为我必须在 中指定特定函数action,例如:

cameraButton.addTarget(delegate, action: #selector(AAPLViewController.cameraButtonDidPress), for: .touchUpInside)

如果我想通过一个按钮定位多个客户端,如何解决这个问题?

4

2 回答 2

5

如果您制定协议,它应该可以工作@objc

@objc protocol CameraButtonDelegate: class {
    func cameraButtonDidPress(_ sender: UIButton
}

并将选择器指定为协议方法

cameraButtonDidPress.addTarget(delegate, action: #selector(CameraButtonDelegate.cameraButtonDidPress), for: .touchUpInside)
于 2018-08-14T10:30:09.890 回答
0

你可以试试

cameraButton.addTarget(Service.shared, action: #selector(Service.shared.cameraButtonDidPress(_:)), for: .touchUpInside)

//

class Service {

   static let shared = Service()

    @objc func cameraButtonDidPress (_ sender:UIButton){


    }
}
于 2018-08-14T10:24:41.713 回答