如何在 SwiftUI 中关闭 ResearchKit 模态视图?

How to close ResearchKit Modal view in SwiftUI?

我正在使用 SwiftUI 编写一个供个人使用的研究工具包应用程序,我想知道如何与打开的模态视图交互研究工具包调查任务。

我目前正在使用此代码打开视图:

struct SurveyView: UIViewControllerRepresentable {

typealias UIViewControllerType = ORKTaskViewController

func makeUIViewController(context: Context) -> ORKTaskViewController {

    let taskViewController = ORKTaskViewController(task: SurveyTask, taskRun: nil)
    taskViewController.view.tintColor = UIColor(red:0.64, green:0.15, blue:0.11, alpha:1.00)
    return taskViewController

}

func updateUIViewController(_ taskViewController: ORKTaskViewController, context: Context) {
    }

}

我正在使用一个按钮来调用它,但是我无法使用研究工具包中的取消或完成按钮关闭它,因为我不知道应该在哪里实现 didFinishWithReason reason: ORKTaskViewControllerFinishReason

非常感谢任何帮助。

我已经使用 Coordinators 做到了。如果有人感兴趣,这是代码。

struct SurveyView: UIViewControllerRepresentable {
func makeCoordinator() -> Coordinator {
    Coordinator()
}


typealias UIViewControllerType = ORKTaskViewController

func makeUIViewController(context: Context) -> ORKTaskViewController {

    let taskViewController = ORKTaskViewController(task: SurveyTask, taskRun: nil)
    taskViewController.view.tintColor = UIColor(red:0.64, green:0.15, blue:0.11, alpha:1.00)
    taskViewController.delegate = context.coordinator
    return taskViewController

}

func updateUIViewController(_ taskViewController: ORKTaskViewController, context: Context) {

    }

class Coordinator: NSObject, ORKTaskViewControllerDelegate {
    func taskViewController(_ taskViewController: ORKTaskViewController, didFinishWith reason: ORKTaskViewControllerFinishReason, error: Error?) {
        taskViewController.dismiss(animated: true, completion: nil)
    }
}

}