【问题标题】:How to close ResearchKit Modal view in SwiftUI?如何在 SwiftUI 中关闭 ResearchKit 模态视图?
【发布时间】:2020-07-06 13:28:56
【问题描述】:

我正在使用 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

非常感谢任何帮助。

【问题讨论】:

    标签: swift swiftui researchkit


    【解决方案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)
        }
    }
    

    }

    【讨论】:

    • 我尝试在 makeUIViewController 中创建 ORKInstructionStep 和 ORKOrderedTask,然后将 SurveyView 放入 SwiftUI VStack,但代码崩溃了。我应该怎么做才能让 SurveyView() 在 SwiftUI 视图中工作?
    猜你喜欢
    • 1970-01-01
    • 2019-10-30
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多