Ios Swift-Invoke自定义委托方法
我正在使用库,并希望实现自定义控件,为此,我有以下类,这些类符合以下协议Ios Swift-Invoke自定义委托方法,ios,swift,delegates,swift-protocols,swift-extensions,Ios,Swift,Delegates,Swift Protocols,Swift Extensions,我正在使用库,并希望实现自定义控件,为此,我有以下类,这些类符合以下协议 @objc public protocol BMPlayerControlViewDelegate: class { func controlView(controlView: BMPlayerControlView, didChooseDefition index: Int) func controlView(controlView: BMPlayerControlView, didPressButton
@objc public protocol BMPlayerControlViewDelegate: class {
func controlView(controlView: BMPlayerControlView, didChooseDefition index: Int)
func controlView(controlView: BMPlayerControlView, didPressButton button: UIButton)
func controlView(controlView: BMPlayerControlView, slider: UISlider, onSliderEvent event: UIControlEvents)
@objc optional func controlView(controlView: BMPlayerControlView, didChangeVideoPlaybackRate rate: Float)
}
open class BMPlayerControlView: UIView {
open weak var delegate: BMPlayerControlViewDelegate?
open weak var player: BMPlayer?
// Removed rest of the code for clarity
open func onButtonPressed(_ button: UIButton) {
autoFadeOutControlViewWithAnimation()
if let type = ButtonType(rawValue: button.tag) {
switch type {
case .play, .replay:
if playerLastState == .playedToTheEnd {
hidePlayToTheEndView()
}
default:
break
}
}
delegate?.controlView(controlView: self, didPressButton: button)
}
}
我使用以下代码扩展BMPlayerControlView类以扩展控件视图
class BMPlayerCustomControlStyle3: BMPlayerControlView {
}
class BMPlayerStyle3: BMPlayer {
class override func storyBoardCustomControl() -> BMPlayerControlView? {
return BMPlayerCustomControlStyle3()
}
}
我的问题是,如何调用didPressButton
delegate方法?我不想覆盖按下的按钮,我尝试了以下方法
extension BMPlayerCustomControlStyle3:BMPlayerControlViewDelegate {
func controlView(controlView: BMPlayerControlView, didChooseDefition index: Int) {
}
func controlView(controlView: BMPlayerControlView, didPressButton button: UIButton) {
print("Did Press Button Invoked")
}
func controlView(controlView: BMPlayerControlView, slider: UISlider, onSliderEvent event: UIControlEvents) {
}
}
这似乎不起作用,我还缺什么
谢谢。如果您希望您的
BMPlayerControlView
子类也充当委托对象,您还需要设置delegate
属性(并像您已经做的那样遵守BMPlayerControlViewDelegate
协议)
一种方法是重写子类中的委托
超类属性:
class BMPlayerCustomControlStyle3: BMPlayerControlView {
override open weak var delegate: BMPlayerControlViewDelegate? {
get { return self }
set { /* fatalError("Delegate for internal use only!") */ }
}
}
当然,在这样的内部使用委托时,您根本不允许
BMPlayerControlView
客户端使用它。上面覆盖的集合
可确保您在尝试执行此操作时出错 尝试将super.controlView(controlView:controlView,didPressButton)
放入覆盖的方法中。如果不行,请告诉我。@Ibrahimazharamar您仍然需要保留扩展名BMPlayerCustomControlStyle3:BMPlayerControlViewDelegate..
)你说得对,保罗,我确实错过了,当我添加它时,它给了我另一个错误,上面写着致命错误:委托仅供内部使用代码>我发表了评论,并得到了另一个错误,无法使用仅就绪属性委托重写可变属性
请参阅我的更新答案。。。您可能注释掉了整个setter;)