0

编辑:我终于解决了这个问题,这是缓存问题和缺少代码行的组合。我从未真正将任务添加到视图控制器,不知何故我错过了那一步。但是我在运行 github 演示项目时也遇到了奇怪的错误,必须重新启动并 sudo 删除缓存目录才能让 Xcode 正常运行。


我正在尝试在 Objective-C 中实现研究工具包。没有教程和很少的文档可供参考。我遇到了崩溃“视觉同意步骤没有可见场景”。我有一个视图控制器,在那个视图控制器上我有一个触发 IBAction“consentTapped”的按钮。我试图改编 Ray Wenderlich 教程http://www.raywenderlich.com/104575/researchkit-tutorial-with-swift和这个 GitHub 项目:https://github.com/weberbry/ResearchKitConsentDemo

为了自己解决这个问题,我将所有代码放在 viewDidAppear 中,将其从封装的方法中取出,因为我认为我这样做是错误的,但仍然存在问题:

这是我的代码:

#import "ViewController.h"
#import <ResearchKit/ResearchKit.h>

@interface ViewController ()<ORKTaskViewControllerDelegate>

@property (strong, nonatomic) ORKConsentDocument *consentDocument;
@property (strong, nonatomic) ORKOrderedTask *orderedTask;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];


    NSString *resource = [[NSBundle mainBundle] pathForResource:@"ConsentText" ofType:@"json"];
    NSData *consentData = [NSData dataWithContentsOfFile:resource];
    NSDictionary *parsedConsentData = [NSJSONSerialization JSONObjectWithData:consentData options:NSJSONReadingMutableContainers error:nil];

    NSArray *sectionDataParsedFromInputFile = [parsedConsentData objectForKey:@"sections"];


    NSMutableArray *consentSections = [NSMutableArray new];
    for (NSDictionary *sectionDictionary in sectionDataParsedFromInputFile) {


        ORKConsentSectionType sectionType = [[sectionDictionary objectForKey:@"sectionType"] integerValue];
        NSString *title = [sectionDictionary objectForKey:@"sectionTitle"];
        NSString *summary = [sectionDictionary objectForKey:@"sectionSummary"];
        NSString *detail = [sectionDictionary objectForKey:@"sectionDetail"];

        ORKConsentSection *section = [[ORKConsentSection alloc] initWithType:sectionType];
        section.title = title;
        section.summary = summary;
        section.htmlContent = detail;


        ORKConsentSection *consentSection = section;
        [consentSections addObject:consentSection];
    }


    ORKConsentSection *introSection = [[ORKConsentSection alloc] initWithType:ORKConsentSectionTypeOnlyInDocument];
    introSection.title = @"Intro Language";
    introSection.content = @"This will only be shown in the consent document because this sectionType is map to ORKConsentSectionTypeOnlyInDocument. A consent document can include many sections with type ORKConsentSectionTypeOnlyInDocument. In this document there is a ORKConsentSectionTypeOnlyInDocument section as an intro and one as a closing section";
    [consentSections insertObject:introSection atIndex:0];


    ORKConsentSection *closingSection = [[ORKConsentSection alloc] initWithType:ORKConsentSectionTypeOnlyInDocument];
    closingSection.title = @"Additional Terms";
    closingSection.htmlContent = @"Adding a ORKConsentSectionTypeOnlyInDocument at the end of a consent can be helpful to include any additional legal or related information.";
    [consentSections addObject:closingSection];


    self.consentDocument = [ORKConsentDocument new];
    self.consentDocument.title = @"Demo Consent";
    self.consentDocument.sections = consentSections;


    ORKConsentSignature *signature = [ORKConsentSignature new];
    self.consentDocument.signatures = [NSArray arrayWithObject:signature];


    ORKVisualConsentStep *visualConsentStep = [[ORKVisualConsentStep alloc] initWithIdentifier:@"visualConsentStep" document:self.consentDocument];


    ORKConsentReviewStep *consentReviewStep = [[ORKConsentReviewStep alloc] initWithIdentifier:@"consentReviewStep" signature:self.consentDocument.signatures.firstObject inDocument:self.consentDocument];
    consentReviewStep.text = @"Review Consent!";
    consentReviewStep.reasonForConsent = @"I confirm that I consent to join this study";

    self.orderedTask =  [[ORKOrderedTask alloc] initWithIdentifier:@"consent" steps:@[visualConsentStep, consentReviewStep]];

}

- (IBAction)consentTapped:(id)sender {

    ORKTaskViewController *taskViewController = [[ORKTaskViewController alloc]initWithTask:self.orderedTask taskRunUUID:nil];
    taskViewController.delegate = self;
    [self presentViewController:taskViewController animated:YES completion:nil];
}

- (void)taskViewController:(ORKTaskViewController *)taskViewController
       didFinishWithReason:(ORKTaskViewControllerFinishReason)reason
                     error:(NSError *)error {
    ORKTaskResult *taskResult = [taskViewController result];
    [self dismissViewControllerAnimated:YES completion:nil];
}

由于错误的措辞,我觉得应该有另一个视图控制器场景,但除非我错过了,否则我在 Ray Wenderlich 教程中没有看到它。我还不知道 swift 所以如果你知道并且你发现我错过了一些东西,请告诉我。

当您离开 viewDidAppear 时会发生崩溃。

这也是我在这里的第一篇文章,所以如果我没有遵循社区准则,请告诉我,我会立即修改我的帖子。


编辑:这是工作代码。请记住,sudo 删除您的 DerivedData 文件夹,如果除了我发布的原始错误之外还有奇怪的错误,请重新启动。缺少的行是“taskViewController.task = self.orderedTask;”

#import "ViewController.h"
#import <ResearchKit/ResearchKit.h>

@interface ViewController ()<ORKTaskViewControllerDelegate>

@property (strong, nonatomic) ORKConsentDocument *consentDocument;
@property (strong, nonatomic) ORKOrderedTask *orderedTask;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];


    NSString *resource = [[NSBundle mainBundle] pathForResource:@"ConsentText" ofType:@"json"];
    NSData *consentData = [NSData dataWithContentsOfFile:resource];
    NSDictionary *parsedConsentData = [NSJSONSerialization JSONObjectWithData:consentData options:NSJSONReadingMutableContainers error:nil];

    NSArray *sectionDataParsedFromInputFile = [parsedConsentData objectForKey:@"sections"];

    NSMutableArray *consentSections = [NSMutableArray new];
    for (NSDictionary *sectionDictionary in sectionDataParsedFromInputFile) {

        ORKConsentSectionType sectionType = [[sectionDictionary objectForKey:@"sectionType"] integerValue];
        NSString *title = [sectionDictionary objectForKey:@"sectionTitle"];
        NSString *summary = [sectionDictionary objectForKey:@"sectionSummary"];
        NSString *detail = [sectionDictionary objectForKey:@"sectionDetail"];

        ORKConsentSection *section = [[ORKConsentSection alloc] initWithType:sectionType];
        section.title = title;
        section.summary = summary;
        section.htmlContent = detail;

        ORKConsentSection *consentSection = section;
        [consentSections addObject:consentSection];
    }

    ORKConsentSection *introSection = [[ORKConsentSection alloc] initWithType:ORKConsentSectionTypeOnlyInDocument];
    introSection.title = @"Intro Language";
    introSection.htmlContent = @"This will only be shown in the consent document because this sectionType is map to ORKConsentSectionTypeOnlyInDocument. A consent document can include many sections with type ORKConsentSectionTypeOnlyInDocument. In this document there is a ORKConsentSectionTypeOnlyInDocument section as an intro and one as a closing section";

    [consentSections insertObject:introSection atIndex:0];


    ORKConsentSection *closingSection = [[ORKConsentSection alloc] initWithType:ORKConsentSectionTypeOnlyInDocument];
    closingSection.title = @"Additional Terms";
    closingSection.htmlContent = @"Adding a ORKConsentSectionTypeOnlyInDocument at the end of a consent can be helpful to include any additional legal or related information.";
    [consentSections addObject:closingSection];


    NSArray *sections = consentSections;


    self.consentDocument = [ORKConsentDocument new];
    self.consentDocument.title = @"Demo Consent";
    self.consentDocument.sections = consentSections;

    ORKConsentSignature *signature = [ORKConsentSignature new];
    self.consentDocument.signatures = [NSArray arrayWithObject:signature];


    ORKVisualConsentStep *visualConsentStep = [[ORKVisualConsentStep alloc] initWithIdentifier:@"visualConsentStep" document:self.consentDocument];


    ORKConsentReviewStep *consentReviewStep = [[ORKConsentReviewStep alloc] initWithIdentifier:@"consentReviewStep" signature:self.consentDocument.signatures.firstObject inDocument:self.consentDocument];
    consentReviewStep.text = @"Review Consent!";
    consentReviewStep.reasonForConsent = @"I confirm that I consent to join this study";


    self.orderedTask =  [[ORKOrderedTask alloc] initWithIdentifier:@"consent" steps:@[visualConsentStep, consentReviewStep]];

}

- (IBAction)consentTapped:(id)sender {

    ORKTaskViewController *taskViewController = [[ORKTaskViewController alloc] init];
    taskViewController.task = self.orderedTask;
    taskViewController.delegate = self;
    [self presentViewController:taskViewController animated:YES completion:nil];
}

- (void)taskViewController:(ORKTaskViewController *)taskViewController
       didFinishWithReason:(ORKTaskViewControllerFinishReason)reason
                     error:(NSError *)error {
    ORKTaskResult *taskResult = [taskViewController result];
    [self dismissViewControllerAnimated:YES completion:nil];
}
4

1 回答 1

0

艾比杰克逊的回答:

这是工作代码。请记住,sudo 删除您的DerivedData文件夹,如果除了我发布的原始错误之外还有奇怪的错误,请重新启动。缺少的行是taskViewController.task = self.orderedTask;

#import "ViewController.h"
#import <ResearchKit/ResearchKit.h>

@interface ViewController ()<ORKTaskViewControllerDelegate>

@property (strong, nonatomic) ORKConsentDocument *consentDocument;
@property (strong, nonatomic) ORKOrderedTask *orderedTask;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];


    NSString *resource = [[NSBundle mainBundle] pathForResource:@"ConsentText" ofType:@"json"];
    NSData *consentData = [NSData dataWithContentsOfFile:resource];
    NSDictionary *parsedConsentData = [NSJSONSerialization JSONObjectWithData:consentData options:NSJSONReadingMutableContainers error:nil];

    NSArray *sectionDataParsedFromInputFile = [parsedConsentData objectForKey:@"sections"];

    NSMutableArray *consentSections = [NSMutableArray new];
    for (NSDictionary *sectionDictionary in sectionDataParsedFromInputFile) {

        ORKConsentSectionType sectionType = [[sectionDictionary objectForKey:@"sectionType"] integerValue];
        NSString *title = [sectionDictionary objectForKey:@"sectionTitle"];
        NSString *summary = [sectionDictionary objectForKey:@"sectionSummary"];
        NSString *detail = [sectionDictionary objectForKey:@"sectionDetail"];

        ORKConsentSection *section = [[ORKConsentSection alloc] initWithType:sectionType];
        section.title = title;
        section.summary = summary;
        section.htmlContent = detail;

        ORKConsentSection *consentSection = section;
        [consentSections addObject:consentSection];
    }

    ORKConsentSection *introSection = [[ORKConsentSection alloc] initWithType:ORKConsentSectionTypeOnlyInDocument];
    introSection.title = @"Intro Language";
    introSection.htmlContent = @"This will only be shown in the consent document because this sectionType is map to ORKConsentSectionTypeOnlyInDocument. A consent document can include many sections with type ORKConsentSectionTypeOnlyInDocument. In this document there is a ORKConsentSectionTypeOnlyInDocument section as an intro and one as a closing section";

    [consentSections insertObject:introSection atIndex:0];


    ORKConsentSection *closingSection = [[ORKConsentSection alloc] initWithType:ORKConsentSectionTypeOnlyInDocument];
    closingSection.title = @"Additional Terms";
    closingSection.htmlContent = @"Adding a ORKConsentSectionTypeOnlyInDocument at the end of a consent can be helpful to include any additional legal or related information.";
    [consentSections addObject:closingSection];

    NSArray *sections = consentSections;

    self.consentDocument = [ORKConsentDocument new];
    self.consentDocument.title = @"Demo Consent";
    self.consentDocument.sections = consentSections;

    ORKConsentSignature *signature = [ORKConsentSignature new];
    self.consentDocument.signatures = [NSArray arrayWithObject:signature];

    ORKVisualConsentStep *visualConsentStep = [[ORKVisualConsentStep alloc] initWithIdentifier:@"visualConsentStep" document:self.consentDocument];

    ORKConsentReviewStep *consentReviewStep = [[ORKConsentReviewStep alloc] initWithIdentifier:@"consentReviewStep" signature:self.consentDocument.signatures.firstObject inDocument:self.consentDocument];
    consentReviewStep.text = @"Review Consent!";
    consentReviewStep.reasonForConsent = @"I confirm that I consent to join this study";

    self.orderedTask =  [[ORKOrderedTask alloc] initWithIdentifier:@"consent" steps:@[visualConsentStep, consentReviewStep]];
}

- (IBAction)consentTapped:(id)sender {

    ORKTaskViewController *taskViewController = [[ORKTaskViewController alloc] init];
    taskViewController.task = self.orderedTask;
    taskViewController.delegate = self;
    [self presentViewController:taskViewController animated:YES completion:nil];
}

- (void)taskViewController:(ORKTaskViewController *)taskViewController
       didFinishWithReason:(ORKTaskViewControllerFinishReason)reason
                     error:(NSError *)error {
    ORKTaskResult *taskResult = [taskViewController result];
    [self dismissViewControllerAnimated:YES completion:nil];
}
于 2015-08-24T11:32:08.223 回答