编辑:我终于解决了这个问题,它是缓存问题和缺少代码行的组合。我从未真正将任务添加到视图控制器,不知怎的,我错过了那一步。但是我运行github演示项目时也遇到了奇怪的错误,不得不重新启动并删除缓存目录以使Xcode正常运行。
我正在尝试在Objective-C中实现Research Kit。没有教程和很少的文档可以转向。我正在崩溃“视觉同意步骤没有可见的场景”。我有一个视图控制器,在该视图控制器上我有一个触发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教程中看到一个,除非我错过了它。我还不知道很快,所以如果你这样做,你发现我错过了什么,请告诉我。
当您离开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];
}
答案 0 :(得分:0)
回答 Abbey Jackson :
这是工作代码。请记住,sudo删除 DerivedData 文件夹,如果除了我发布的原始错误之外还有奇怪的错误,请重新启动。缺失的行是
CustomTabs
taskViewController.task = self.orderedTask;