2

Using a UIViewController in SwiftUI I need to assign selectors for two buttons. Those need to be @objc but I get the error:

Method cannot be marked @objc because the type of the parameter cannot be represented in Objective-C

struct ScanPreView: UIViewControllerRepresentable {

    class Coordinator: NSObject {
        
        var parent: ScanPreView
        
        init(_ parent: ScanPreView) {
            self.parent = parent
        }
        
        @objc func dismissPreviewedScanTapped(sender: ScanPreView) {
            
        }
        
        @objc func savePreviewedSceneTapped(sender: ScanPreView) {
            
        }
    }
        
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    func makeUIViewController(context: Context) -> ScenePreviewViewController {
        let preview = ScenePreviewViewController(pointCloud: pointCloud, meshTexturing: meshTexturing, landmarks: nil)
        preview.leftButton.addTarget(context.coordinator, action: #selector(Coordinator.dismissPreviewedScanTapped(sender:)), for: UIControl.Event.touchUpInside)
        preview.rightButton.addTarget(context.coordinator, action: #selector(Coordinator.savePreviewedSceneTapped(sender:)), for: UIControl.Event.touchUpInside)
        return preview
    }
}
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Mike Koene
  • 330
  • 3
  • 15

1 Answers1

2

You don't need view there, so just use Any if you need action signature, like

@objc func dismissPreviewedScanTapped(sender: Any) {
    
}
Asperi
  • 228,894
  • 20
  • 464
  • 690