1

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?

asheyla
  • 3,175
  • 5
  • 18
  • 34

1 Answers1

1

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)
}
Ricardo Sanchez-Saez
  • 9,466
  • 8
  • 53
  • 92
  • Yes, it worked out like that for me. I did something like this: func taskViewController(taskViewController: ORKTaskViewController, didFinishWithReason reason: ORKTaskViewControllerFinishReason, error: NSError?) { switch reason { case .Completed: ... case .Saved, .Failed, .Discarded: taskViewController.dismissViewControllerAnimated(true, completion: nil) break } – asheyla Mar 22 '16 at 13:53