编辑:不确定这是否可能是我问题的根源,但是应用程序委托中的这段代码会是这不起作用的原因吗?
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!
看起来您还没有扩展视图控制器来实现ORKTaskViewControllerDelegate
ORKTaskViewController
委托,您的 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?) {
}
}