swift - 类型 'MasterViewController' 不符合协议(protocol) 'CropPerformanceControllerDelegate'

标签 swift delegates

在我的应用程序上,有多个 View ,您可以在其中输入某些值(设置),因此这些设置会使用委托(delegate)发送回主视图。我已经有了另外 2 个 View 的设置,只需复制并粘贴所需的代码并更改变量,但这会引发错误; “类型‘MasterViewController’不符合协议(protocol)‘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!)
        }
    }
}

因此,这个完全相同的代码用于设置和降雨数据 View ,没有问题,但是现在在主视图上,“CropPerformanceControllerDelegate”似乎无法被识别,并且类“CropPerformance”的任何使用都会导致错误; “使用未声明的类型‘CropPerformance’”。我希望这是足够的信息,所有与委托(delegate)有关的代码都已发布,所有其他不必要的变量声明和函数我都省略了。

我寻找了其他答案,他们都说如果你想遵守协议(protocol),你需要实现所有必需的方法。这到底是什么意思呢?我的所有函数都在我的类中,当我删除有关委托(delegate)的部分时,代码可以完美运行。

提前致谢。

最佳答案

我无法解决问题,因为代码中没有错误,我只是从头开始,制作了一个新文档,复制并粘贴相同的代码,将名称更改为 CropPerformance2 并且它起作用了。 (它适用于除 CropPerformance 之外的任何其他名称)。这很奇怪,肯定与我删除的初始类“CropPerformace”有关,因为它没有从系统内存中完全删除,至少这是我的假设。总之,没有答案,我只是重新创建了一切,它起作用了。

关于swift - 类型 'MasterViewController' 不符合协议(protocol) 'CropPerformanceControllerDelegate',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32253482/

相关文章:

ios - 如何创建具有内部框架的框架?

ios - 在 iOS 中应用模糊滤镜时如何从图像中删除黑色阴影矩形?

使用 ScrollView 快速重新创建 iPhone 应用程序切换器页面

arrays - 在 Swift 中添加两个数组的元素而不附加在一起

iphone - 委托(delegate)不工作(与 Restkit 相关?)

swift - 委托(delegate)回传不起作用

design-patterns - qml 文件夹列表模型

c# - 编译委托(delegate)和调度程序时出错

c# - Simple Delegate(委托(delegate))与多播委托(delegate)

swift - RAC 4 Swift 失败方法