3

I'd like to have a protocol:

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

So, I could assign any client to a button, e.g.:

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

However, it does not compile as I have to specify a particular function in action, e.g.:

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

How to solve this issue, if I'd like to have multiple clients being targeted by a single button?

vacawama
  • 150,663
  • 30
  • 266
  • 294
Richard Topchii
  • 7,075
  • 8
  • 48
  • 115

2 Answers2

7

It should work if you make the protocol @objc

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

and specify the selector as protocol method

cameraButtonDidPress.addTarget(delegate, action: #selector(CameraButtonDelegate.cameraButtonDidPress), for: .touchUpInside)
vadian
  • 274,689
  • 30
  • 353
  • 361
0

You can try

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

//

class Service {

   static let shared = Service()

    @objc func cameraButtonDidPress (_ sender:UIButton){


    }
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87