Search code examples
objective-cswiftswiftuiuiviewcontrollerrepresentable

SwiftUI Coordinator: method can not be marked @objc


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

Solution

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

    @objc func dismissPreviewedScanTapped(sender: Any) {
        
    }