使用 ResearchKit 以编程方式呈现 ORKTaskViewController



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

使用 ResearchKit 上的 Ray Wenderlich 教程,我创建了一个调查任务:

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

我的视图控制器中的委托设置如下:

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

然后在该视图控制器中,我尝试呈现ORKTaskViewController

@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。我试图搜索这个的无故事板实现,但我能找到的最接近的是这个仍然使用故事板。任何帮助将不胜感激。

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

ORKNavigationContainerView.m的这一行代码:

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

不起作用,我假设这是因为 iOS 13 中引入了SceneDelegate.swift,这是我以编程方式设置窗口和根视图控制器的地方(而不是在 AppDelegate 中执行此操作(。

如果将其更改为如下所示的内容:

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-c,但我试图获取实例化新视图控制器的视图窗口,它似乎可以工作(随意编辑(。

相关内容

  • 没有找到相关文章

最新更新