编辑:不确定这是否可能是我问题的根源,但是应用程序委托中的这段代码是否会导致此问题不起作用?
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!
最佳答案
看起来您尚未扩展视图控制器以实现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?) {
}
}
关于ios - 在ViewController中实现ResearchKit进行调查,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35591974/