![](/img/trans.png)
[英]How do I dismiss an ORKTaskViewController and present a view controller of my choice?
[英]present ORKTaskViewController programmatically using ResearchKit
我從 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)
}
我的 ViewController 中的委托設置如下:
extension ViewController: ORKTaskViewControllerDelegate {
func taskViewController(_ taskViewController: ORKTaskViewController, didFinishWith reason: ORKTaskViewControllerFinishReason, error: Error?) {
taskViewController.dismiss(animated: true, completion: nil)
}
}
然后在那個 ViewController 中,我嘗試展示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
,我不太清楚為什么。 我沒有使用故事板,但我不明白為什么沒有 storyboard 就無法使用 ResearchKit。 我試圖搜索它的無情節提要實現,但我能找到的最接近的是它仍然使用 storyboard。 任何幫助,將不勝感激。
添加異常斷點后,我發現它與ResearchKit庫中的_appTintColor
有關。
ORKNavigationContainerView.m
中的這行代碼:
_appTintColor = [[UIApplication sharedApplication].delegate window].tintColor;
did not work and I'm assuming this is because SceneDelegate.swift
was introduced in iOS 13, which is where I'm programmatically setting the window and root view controller (as opposed to doing it in 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,但我試圖獲得實例化新視圖 controller 的視圖的 window,它似乎可以工作(隨意編輯)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.