如何在SwiftUI中关闭ResearchKit Modal视图?

时间:2020-03-25 22:35:40

标签: swift swiftui researchkit

我正在使用SwiftUI编写供个人使用的研究套件应用程序,我想知道如何与Modal View打开的研究套件调查任务进行交互。

此刻我正在使用以下代码打开视图:

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

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我已经设法使用协调器做到了。如果有人感兴趣,这里是代码。

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

}