Search code examples
iosswiftcancel-buttonresearchkit

ResearchKit Cancel button not working


I am working on a project(Swift) using the ResearchKit and my Cancel bar button is not working. I have found the following methods that should make it work

- (void)setCancelButtonItem:(UIBarButtonItem *)cancelButtonItem {
    [super setCancelButtonItem:cancelButtonItem];
    [cancelButtonItem setTarget:self];
    [cancelButtonItem setAction:@selector(cancelButtonHandler:)];
}
- (void)cancelButtonHandler:(id)sender {
    STRONGTYPE(self.taskViewController.delegate) strongDelegate = self.taskViewController.delegate;
    if ([strongDelegate respondsToSelector:@selector(taskViewController:didFinishWithReason:error:)]) {
        [strongDelegate taskViewController:self.taskViewController didFinishWithReason:ORKTaskViewControllerFinishReasonDiscarded error:nil];
    }
}

I get the Discard Results and Cancel popup, but nothing happens when I tap the Discard Results option.

Should I check for something else? Should I connect it somewhere?


Solution

  • Clicking that button should call the taskViewController(taskViewController: ORKTaskViewController, didFinishWithReason reason: ORKTaskViewControllerFinishReason, error: NSError?) method in your task view controller delegate. You have to manually dismiss the task view controller there.

    See, for example, the implementation in ORKCatalog's TaskListViewController.swift:

    func taskViewController(taskViewController: ORKTaskViewController, didFinishWithReason reason: ORKTaskViewControllerFinishReason, error: NSError?) {
        /*
            The `reason` passed to this method indicates why the task view
            controller finished: Did the user cancel, save, or actually complete
            the task; or was there an error?
    
            The actual result of the task is on the `result` property of the task
            view controller.
        */
        taskResultFinishedCompletionHandler?(taskViewController.result)
    
        taskViewController.dismissViewControllerAnimated(true, completion: nil)
    }