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

Posted

技术标签:

【中文标题】如何在 SwiftUI 中关闭 ResearchKit 模态视图?【英文标题】:How to close ResearchKit Modal view in SwiftUI? 【发布时间】: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

非常感谢任何帮助。

【问题讨论】:

【参考方案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 视图中工作?

以上是关于如何在 SwiftUI 中关闭 ResearchKit 模态视图?的主要内容,如果未能解决你的问题,请参考以下文章

在 SwiftUI 中关闭按钮突出显示

SwiftUI 2 - 如何防止顶部部分在 macOS 和 iPadOS 的列表中关闭

在 SwiftUI 中关闭 WKWebView

SwiftUI:在 macOS NavigationView 中关闭视图

SwiftUI 从 UITextField 中关闭键盘

如何在线程中关闭 QWebSocket?