0

编辑:不确定这是否是我问题的根源,但应用程序委托中的这段代码是否会成为它不起作用的原因?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    let navigationController = self.window!.rootViewController as! UINavigationController
    let controller = navigationController.topViewController as! HomeViewController
    controller.managedObjectContext = self.managedObjectContext
    return true

}

我正在尝试将调查功能添加到集成了 ResearchKit 的应用程序中。我已经阅读了 Ray Wenderlich 的设置指南和其他一些教程。然而,当我过渡到一个我想开发的应用程序时,我有点卡住了。

我收到一个错误: Cannot assign value of type 'HomeviewController to type 'ORKTaskViewControllerDelegate?'

这是我正在使用的代码:

class HomeViewController: UIViewController {
var managedObjectContext: NSManagedObjectContext?

@IBAction func surveyTapped(sender: AnyObject) {

    let taskViewController = ORKTaskViewController(task: SurveyTask, taskRunUUID: nil)
    taskViewController.delegate = self
    presentViewController(taskViewController, animated: true, completion: nil)

}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.destinationViewController.isKindOfClass(NewRideViewController) {
        if let newRideViewController = segue.destinationViewController as? NewRideViewController {
            newRideViewController.managedObjectContext = managedObjectContext
        }
    }
}
}

基于与此错误语法类似的问题,用户添加了另一个控制器类,但我超出了我的范围。非常感谢您的帮助,谢谢 StackExchange!

4

1 回答 1

1

看起来你还没有扩展你的视图控制器来实现ORKTaskViewController它的委托,ORKTaskViewControllerDelegate你的 VC 代码应该如下 -

import ResearchKit

class HomeVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func surveyTapped(sender: AnyObject) {
        let taskViewController = ORKTaskViewController(task: SurveyTask, taskRunUUID: nil)
        taskViewController.delegate = self
        presentViewController(taskViewController, animated: true, completion: nil)
    }

}

extension HomeVC: ORKTaskViewControllerDelegate {

    func taskViewController(taskViewController: ORKTaskViewController, didFinishWithReason reason: ORKTaskViewControllerFinishReason, error: NSError?) {

    }

}
于 2016-02-24T07:16:32.267 回答