首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ORKTaskViewController以编程方式显示ResearchKit

使用ORKTaskViewController以编程方式显示ResearchKit
EN

Stack Overflow用户
提问于 2019-10-21 04:09:29
回答 1查看 371关注 0票数 0

我从最新发布的ResearchKit下载了GitHub,并将框架嵌入到我的项目中。

使用雷·温德利希ResearchKit教程,我创建了一个调查任务:

代码语言:javascript
复制
import ResearchKit

public var SurveyTask: ORKOrderedTask {

    var steps = [ORKStep]()

    let instructionStep = ORKInstructionStep(identifier: "IntroStep")
    instructionStep.title = "The Questions Three"
    instructionStep.text = "Who would cross the Bridge of Death must answer me these questions three, ere the other side they see."
    steps += [instructionStep]

    let nameAnswerFormat = ORKTextAnswerFormat(maximumLength: 20)
    nameAnswerFormat.multipleLines = false
    let nameQuestionStepTitle = "What is your name?"
    let nameQuestionStep = ORKQuestionStep(identifier: "QuestionStep", title: nameQuestionStepTitle, question: "Hello?", answer: nameAnswerFormat)
    steps += [nameQuestionStep]

    let questQuestionStepTitle = "What is your quest?"
    let textChoices = [
      ORKTextChoice(text: "Create a ResearchKit App", value: 0 as NSNumber),
      ORKTextChoice(text: "Seek the Holy Grail", value: 1 as NSNumber),
      ORKTextChoice(text: "Find a shrubbery", value: 2 as NSNumber)
    ]
    let questAnswerFormat: ORKTextChoiceAnswerFormat = ORKAnswerFormat.choiceAnswerFormat(with: .singleChoice, textChoices: textChoices)
    let questQuestionStep = ORKQuestionStep(identifier: "TextChoiceQuestionStep", title: questQuestionStepTitle, question: "Hello?", answer: questAnswerFormat)
    steps += [questQuestionStep]

    let summaryStep = ORKCompletionStep(identifier: "SummaryStep")
    summaryStep.title = "Right. Off you go!"
    summaryStep.text = "That was easy!"
    steps += [summaryStep]

    return ORKOrderedTask(identifier: "SurveyTask", steps: steps)
}

我的ViewController中的委托设置如下:

代码语言:javascript
复制
extension ViewController: ORKTaskViewControllerDelegate {

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

}

然后在那个ViewController里,我试着展示ORKTaskViewController

代码语言:javascript
复制
@objc func showSurvey() {
    let taskViewController = ORKTaskViewController(task: SurveyTask, taskRun: nil)
    taskViewController.delegate = self
    self.present(taskViewController, animated: true, completion: nil)
}

我一直得到libc++abi.dylib: terminating with uncaught exception of type NSException,但我不太清楚为什么。我不使用故事板,但我不明白为什么没有故事板我就不能使用ResearchKit。我已经尝试过寻找故事板的实现,但我能找到的最接近的是,它仍然使用故事板。任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-25 18:24:09

在添加了一个异常断点之后,我发现它与_appTintColor库中的ResearchKit有关。

ORKNavigationContainerView.m中的这一行代码

_appTintColor = [[UIApplication sharedApplication].delegate window].tintColor;

没有工作,我认为这是因为SceneDelegate.swift是在iOS 13中引入的,我在这里以编程方式设置窗口和根视图控制器(而不是在AppDelegate中这样做)。

如果您将其更改为这样的内容:

代码语言:javascript
复制
if (@available(iOS 13.0, *)) {
    _appTintColor = [self window] .tintColor;
    // not sure if it's this or [[self window].windowScene windows].firstObject .tintColor;
} else {
    _appTintColor = [[UIApplication sharedApplication].delegate window].tintColor;
}

那就应该管用了。我不熟悉Obj,但我试图获得实例化新视图控制器的视图窗口,它似乎可以工作(请随意编辑)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58479691

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档