编辑:不确定这可能是我的问题的根源,但是应用代理中的这段代码是否会导致无效?
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!
答案 0 :(得分: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?) {
}
}