Created
August 5, 2015 05:04
-
-
Save chrishinds/66b18f4af0c69130d7b5 to your computer and use it in GitHub Desktop.
Example in Swift of subclassing an ORKActiveStepViewController in Apple ResearchKit, then laying out a custom view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import ResearchKit | |
class DemoView: UIView { | |
} | |
class DemoStepViewController : ORKActiveStepViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.view.backgroundColor = UIColor(red:0.0, green:1.0, blue:0.0, alpha:1.0) | |
let demoView = DemoView() | |
demoView.setTranslatesAutoresizingMaskIntoConstraints(false) | |
demoView.backgroundColor = UIColor(red:1.0, green:0.0, blue:0.0, alpha:1.0) | |
self.customView = demoView | |
self.customView?.superview!.setTranslatesAutoresizingMaskIntoConstraints(false) | |
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[demoView]-|", options: NSLayoutFormatOptions(0), metrics: nil, views: ["demoView": demoView])) | |
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[demoView]-|", options: NSLayoutFormatOptions(0), metrics: nil, views: ["demoView": demoView])) | |
} | |
} | |
class DemoStep : ORKActiveStep { | |
static func stepViewControllerClass() -> DemoStepViewController.Type { | |
return DemoStepViewController.self | |
} | |
} | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { | |
let activeStep = DemoStep(identifier: "ds_id") | |
activeStep.title = "Demo Step" | |
var endStep = ORKCompletionStep(identifier: "es_id") | |
endStep.title = "Well done" | |
endStep.text = "thank you" | |
let task = ORKOrderedTask(identifier: "ts_id", steps: [activeStep,endStep]) | |
let taskViewController = ORKTaskViewController(task: task, taskRunUUID: nil) | |
taskViewController.delegate = self | |
taskViewController.outputDirectory = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String, isDirectory: true) | |
window?.rootViewController = taskViewController | |
return true | |
} | |
} | |
extension AppDelegate : ORKTaskViewControllerDelegate { | |
func taskViewController(taskViewController: ORKTaskViewController, didFinishWithReason reason: ORKTaskViewControllerFinishReason, error: NSError?) { | |
//Handle results with taskViewController.result | |
taskViewController.dismissViewControllerAnimated(true, completion: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment