在我的应用程序中,有几个视图,您输入某些值(设置),因此这些设置将使用委托发送回主视图。我已经使用其他2个视图进行了此设置,只需复制并粘贴所需的代码并更改变量,但这会带来错误; “Type'MasterViewController'不符合协议'CropPerformanceControllerDelegate'”代码如下:
// MasterViewController
class MasterViewController: UIViewController, SettingsControllerDelegate, RainfallDataControllerDelegate, CropPerformanceControllerDelegate {
// Other variables/functions...
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "MasterToSettings" {
let vc = segue.destinationViewController as! Settings
vc.delegate = self
}
if segue.identifier == "MasterToRainfallData" {
let vc = segue.destinationViewController as! RainfallData
vc.delegate = self
}
if segue.identifier == "MasterToCropPerformance" {
let vc = segue.destinationViewController as! CropPerformance
vc.delegate = self
}
}
func cropPerformanceSettingsSaved(controller: CropPerformance, irrigationResponseFactor: Double, wdImpactFactor: Double, potentialYield: Double) {
controller.navigationController?.popViewControllerAnimated(true)
irrigationResponseFactorM = irrigationResponseFactor
wdImpactFactorM = wdImpactFactor
potentialYieldM = potentialYield
}
}
// New View
// CropPerformanceView
protocol CropPerformanceControllerDelegate {
func cropPerformanceSettingsSaved(controller: CropPerformance, irrigationResponseFactor: Double, wdImpactFactor: Double, potentialYield: Double)
}
class CropPerformance: UIViewController {
var delegate: CropPerformanceControllerDelegate? = nil
// Other functions and Variables
@IBAction func updateCropSettings(sender: AnyObject) {
// Other stuff
if (delegate != nil) {
delegate!.cropPerformanceSettingsSaved(self, irrigationResponseFactor: irrigationResponseFactor!, wdImpactFactor: wdImpactFactor!, potentialYield: potentialYield!)
}
}
}
所以这个完全相同的代码用于设置和rainfallData视图并且没有问题,但是现在在主视图上似乎没有识别'CropPerformanceControllerDelegate'并且类“CropPerformance”的任何使用都会导致错误; “使用未申报的'CropPerformance'”。我希望这是足够的信息,所有与代理相关的代码都被发布,所有其他不必要的变量声明和函数我都遗漏了。
我曾寻找其他答案,他们都说如果你想要符合协议,你需要实现所有必需的方法。这到底是什么意思呢?我的所有函数都在我的类中,当我删除有关委托的部分时,代码工作正常。
提前致谢。
答案 0 :(得分:0)
我无法解决问题,因为代码中没有错误,我只是从头开始,制作一个新文档,复制并粘贴相同的代码,将名称更改为CropPerformance2并且它有效。 (除了CropPerformance之外,它适用于任何其他名称)。这是非常奇怪的,并且必须与我删除的初始类“CropPerformace”有关,因为它没有完全从系统内存中消失,至少这是我的假设。总而言之,没有答案,我只是重新创造了一切,并且它起作用了。