我希望在另一个类中的变量也被重置后,步进器和标签重置为零。变量重置但步进器和标签在使用委托后甚至都没有。
查看控制器:
class ViewController: UIViewController, CircleViewDelegate {
var colors = CircleView()
@IBOutlet weak var circleView1: CircleView!
@IBOutlet weak var redStepper: UIStepper!
@IBOutlet weak var redValue: UILabel!
@IBAction func stepperChange(sender: UIStepper)
{
circleView1.redd1 = Int(redStepper.value);
redValue.text = Int(sender.value).description;
}
func updateRedStepperValue(value: Double) {
redStepper.value = value
redValue.text = Int(colors.redd1.value).description;
}
override func viewDidLoad() {
super.viewDidLoad()
colors.delegate = self
}
}
CircleView:
protocol CircleViewDelegate
{
func updateRedStepperValue(value: Double)
func updateGreenStepperValue(value: Double)
func updateBlueStepperValue(value: Double)
}
class CircleView: UIView
{
var delegate: CircleViewDelegate?
var redd1 = 0
func updateValues()
{
if(redd1==Int(red1))
{
redd1=0;
delegate?.updateRedStepperValue(0.0)//
}
}
}
答案 0 :(得分:2)
问题在于您制作了CircleView的全新实例。
let cycle = CircleView()
您需要将委托设置为当前的工作实例。
为此,您应该使用以下内容替换viewDidLoad中的作业:
override func viewDidLoad() {
super.viewDidLoad()
let app = UIApplication.sharedApplication().delegate! as! AppDelegate
if let viewControllers = app.window?.rootViewController?.childViewControllers {
viewControllers.forEach { vc in
if let cont = vc as? CircleView {
cont.delegate = self
}
}
}
}