在自定义uiview和自定义uicontrol之间设置代理(swift)
问题描述:
import UIKit
public class SelectionItem: UIView {
var radioButtonController: SSRadioButtonsController
var stepper: KWStepper!
public init(){
let gcrRadioButton = SSRadioButton(frame: CGRect(x: 0, y: 100, width: 100, height: 50))
var decrementButton = UIButton(frame: CGRect(x: 400, y: 100, width: 50, height: 50))
var incrementButton = UIButton(frame: CGRect(x: 600, y: 100, width: 50, height: 50))
var countLabel = UILabel(frame: CGRect(x:500,y:100,width:50,height:50))
decrementButton.setImage(UIImage(named: "[email protected]"), forState: UIControlState.Normal)
incrementButton.setImage(UIImage(named: "[email protected]"), forState: UIControlState.Normal)
radioButtonController = SSRadioButtonsController(buttons: gcrRadioButton)
stepper = KWStepper(decrementButton: decrementButton, incrementButton: incrementButton)
stepper.delegate = self
stepper.valueChangedCallback = { [unowned self] stepper in
countLabel.text = String(format: "%.f", stepper.value)
}
super.init(frame: CGRectZero)
self.addSubview(gcrRadioButton)
self.addSubview(decrementButton)
self.addSubview(incrementButton)
}
required public init?(coder aDecoder: NSCoder) {
fatalError("KWStepper: NSCoding is not supported!")
}
}
问题:在stepper.delegate = self时发生错误。无法将类型SelectionItem的值分配给键入KWStepperDelegate。 KWStepper是一个定制的uicontrol类。我如何设置其中两个之间的代表团。在自定义uiview和自定义uicontrol之间设置代理(swift)
答
您将委托设置为self,但是由于书面自我未实施委托协议。它应该是:
public class SelectionItem: UIView, KWStepperDelegate
然后您的SelectionItem必须实现委托协议。