2016-11-04 4 views
0

Следующий код работал частично. Он отображает шаг и форму вопроса. Пользователь смог ввести данные, но когда пользователь нажимал «ничего», ничего не получает в хранилище сохранения, и дисплей не обновляется. Есть идеи?Код симптома Apple CareKit в swift не сохраняет данные в CareStore

class SymptomsVC1: UIViewController{ 


fileprivate let carePlanStoreManager = CarePlanStoreManager1.sharedCarePlanStoreManager 
fileprivate let carePlanData: CarePlanData 
fileprivate var symptomTrackerViewController: OCKSymptomTrackerViewController? = nil 



required init?(coder aDecoder: NSCoder) { 
    carePlanData = CarePlanData(carePlanStore: carePlanStoreManager.store) 

    super.init(coder: aDecoder) 

} 



override func viewDidLoad() { 
    super.viewDidLoad() 
    setViewControllerTitle(self, title: "Symptoms Card") 
    //creatMenuObject(self) 


    let symptomTracker = OCKSymptomTrackerViewController.init(carePlanStore: carePlanStoreManager.store) 
    symptomTracker.progressRingTintColor = UIColor.magenta 
    symptomTracker.delegate = self 
    symptomTracker.showEdgeIndicators = true 
    // Setup the controller's title 
    symptomTracker.title = NSLocalizedString("Symptoms Card", comment: "") 

    //change left navigation "Back" button to menu button 
    var backImage:UIImage = UIImage(named: "menu")! 
    backImage = backImage.withRenderingMode(UIImageRenderingMode.alwaysOriginal) 
    let fakeBackButton = UIBarButtonItem(image: backImage, style: UIBarButtonItemStyle.bordered, target: symptomTracker.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:))) 
    symptomTracker.navigationItem.leftBarButtonItem = fakeBackButton; 

    self.navigationController?.pushViewController(symptomTracker, animated: true) 


} 



override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


extension SymptomsVC1: OCKSymptomTrackerViewControllerDelegate { 

    func symptomTrackerViewController(_ viewController: OCKSymptomTrackerViewController, didSelectRowWithAssessmentEvent assessmentEvent: OCKCarePlanEvent) { 

if viewController.progressRingTintColor == UIColor.magenta { 

    guard let userInfo = assessmentEvent.activity.userInfo, 
     let task: ORKTask = userInfo["ORKTask"] as? ORKTask else { return } 

    let taskViewController = ORKTaskViewController(task: task, taskRun: nil) 
    taskViewController.delegate = self 

    present(taskViewController, animated: true, completion: nil) 


    } 
    } 
} 

extension SymptomsVC1: ORKTaskViewControllerDelegate { 

func taskViewController(_ taskViewController: ORKTaskViewController, didFinishWith 
reason: ORKTaskViewControllerFinishReason, error: Error?) { 

defer { 
    dismiss(animated: true, completion: nil) 
} 
print("task view controller clicked") 
guard reason == .completed else { return } 
guard let symptomTrackerViewController = symptomTrackerViewController, 
     let event = symptomTrackerViewController.lastSelectedAssessmentEvent else { return } 

let carePlanResult = carePlanStoreManager.buildCarePlanResultFrom(taskResult: taskViewController.result) 
print("care plan result") 
print(carePlanResult) 
carePlanStoreManager.store.update(event, with: carePlanResult, state: .completed) { 
    success, _, error in 
    if !success { 
     print(error?.localizedDescription) 
    } 
    } 
} 
} 

ответ

0

Я нашел исправить, добавив одну строку кода в следующей функции:

override func viewDidLoad() { 
super.viewDidLoad() 
setViewControllerTitle(self, title: "Symptoms Card") 
//creatMenuObject(self) 


let symptomTracker = OCKSymptomTrackerViewController.init(carePlanStore: carePlanStoreManager.store) 
symptomTracker.progressRingTintColor = UIColor.magenta 
symptomTracker.delegate = self 
symptomTracker.showEdgeIndicators = true 
// Setup the controller's title 
symptomTracker.title = NSLocalizedString("Symptoms Card", comment: "") 

//*** add the following line, now the result show up in carestore *** 
symptomTrackerViewController = symptomTracker 


//change left navigation "Back" button to menu button 
var backImage:UIImage = UIImage(named: "menu")! 
backImage = backImage.withRenderingMode(UIImageRenderingMode.alwaysOriginal) 
let fakeBackButton = UIBarButtonItem(image: backImage, style: UIBarButtonItemStyle.bordered, target: symptomTracker.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:))) 
symptomTracker.navigationItem.leftBarButtonItem = fakeBackButton; 

self.navigationController?.pushViewController(symptomTracker, animated: true) 

}