ViewController不符合自定义UIButton类

时间:2015-01-13 15:37:36

标签: ios iphone swift uiviewcontroller uibutton

我有一个自定义的UIButton类,我正在尝试导入到项目中的ViewController类中,但是我收到错误消息:'ViewController'不符合协议'OverrideButtonDelegate'“

以下是自定义UIButton类的代码:

import Foundation
import UIKit

protocol OverrideButtonDelegate: NSObjectProtocol {
func overrideButtonDidStartPress(overrideButton: OverrideButton)
func overrideButtonDidEndPress(overrideButton: OverrideButton)
}

class OverrideButton: UIButton {
var delegate: OverrideButtonDelegate?

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    delegate?.overrideButtonDidStartPress(self)
}

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
    delegate?.overrideButtonDidEndPress(self)
}
}

以下是来自ViewController的代码,我收到错误消息“Type'ViewController'不符合协议'OverrideButtonDelegate'”

class ViewController: UIViewController, OverrideButtonDelegate {

1 个答案:

答案 0 :(得分:0)

您是否在UIViewController中的委托中定义了两个函数? 当一个类符合协议时,协议中的函数必须在类中实现

class ViewController: UIViewController, OverrideButtonDelegate {

    func overrideButtonDidStartPress(overrideButton: OverrideButton)
    {
        //Code
    }
    func overrideButtonDidEndPress(overrideButton: OverrideButton)
    {
                    //Code
    }
}