【问题标题】:ViewController not conforming to a custom UIButton ClassViewController 不符合自定义 UIButton 类
【发布时间】:2015-01-13 15:37:35
【问题描述】:

我有一个自定义 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 的代码,其中我收到错误消息“类型 'ViewController' 不符合协议 'OverrideButtonDelegate'”

class ViewController: UIViewController, OverrideButtonDelegate {

【问题讨论】:

    标签: ios iphone swift uiviewcontroller uibutton


    【解决方案1】:

    你在UIViewController的delegate中定义了这两个函数吗? 当类符合协议时,协议中的功能必须在类中实现

    class ViewController: UIViewController, OverrideButtonDelegate {
    
        func overrideButtonDidStartPress(overrideButton: OverrideButton)
        {
            //Code
        }
        func overrideButtonDidEndPress(overrideButton: OverrideButton)
        {
                        //Code
        }
    }
    

    【讨论】:

    • 非常感谢您的帮助。这正是我所缺少的。
    猜你喜欢
    • 2017-10-15
    • 2014-11-03
    • 2019-03-17
    • 1970-01-01
    • 1970-01-01
    • 2020-06-29
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多