【问题标题】:Protocol adding sound to UIButton touchesBegan向 UIButton touchesBegan 添加声音的协议
【发布时间】:2016-04-11 04:14:55
【问题描述】:

我创建了一个协议,添加了一个播放声音的功能:

protocol ButtonSound { }

extension ButtonSound where Self: UIButton {
    func clickSound() { AudioServicesPlaySystemSound(1104) }
}

但是使用这个,我必须手动为每个自定义按钮类添加 clickSoundtouchesBegan

是否可以对仅符合ButtonSound 协议的UIButton 进行扩展并在那里覆盖touchesBegan

【问题讨论】:

  • 正如 Apple 在其面向协议的编程视频中所说的那样,“不要与系统作对”。 Cocoa Touch 是面向对象的。有时仅子类化是有意义的。既然你真的想覆盖超类,为什么不这样做呢?

标签: ios swift uibutton protocols


【解决方案1】:

不,您不能在协议扩展中覆盖超类方法。正如其他人所说,这是子类化的一个案例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-13
    相关资源
    最近更新 更多