gpt4 book ai didi

ios - swift 委托(delegate)不工作

转载 作者:搜寻专家 更新时间:2023-10-31 22:33:10 29 4
gpt4 key购买 nike

我的 ViewController 中有一个步进器,用于控制 UIView 中变量的值。当我在 UIView 中将变量重置为 0 时,我也希望步进器和标签重置,但它们不会。我尝试使用委托(delegate),但出现错误。提前致谢!

View Controller :

protocol CircleViewDelegate: class {
func updateStepper(view: CircleView)
}

class ViewController: UIViewController, CircleViewDelegate {

var colors = CircleView()

@IBOutlet weak var circleView1: CircleView!
@IBOutlet weak var blueStepper: UIStepper!
@IBOutlet weak var greenStepper: UIStepper!
@IBOutlet weak var redStepper: UIStepper!

@IBOutlet weak var redValue: UILabel!
@IBOutlet weak var greenValue: UILabel!
@IBOutlet weak var blueValue: UILabel!

var circleViewDelegate: CircleView!

override func viewDidLoad() {
super.viewDidLoad()
circleViewDelegate!.delegate = self
}

func updateStepper(view: CircleView) {
redStepper.value=0.0;
greenStepper.value=0.0;
blueStepper.value=0.0;
}

@IBAction func stepperChange(sender: UIStepper) {
circleView1.redd1 = Int(redStepper.value);
redValue.text = Int(sender.value).description;
}

@IBAction func stepperChange1(sender: UIStepper) {
circleView1.greenn1 = Int(greenStepper.value);
greenValue.text = Int(sender.value).description;
}

@IBAction func stepperChange2(sender: UIStepper) {
circleView1.bluee1 = Int(blueStepper.value);
blueValue.text = Int(sender.value).description;
}

界面 View :

class CircleView: UIView {    
var redd1 = 0
var greenn1 = 0
var bluee1 = 0

weak var delegate: CircleViewDelegate?

func updateStepper() {
delegate?.updateStepper(self)
}

func game() {
if(redd1==Int(red1) && greenn1==Int(green1) && bluee1==Int(blue1)) {
redd1 = 0;
green1 = 0;
blue1 = 0;
updateStepper()
}
}
}

最佳答案

试试这个:

override func viewDidLoad() {
super.viewDidLoad()
circleView1.delegate = self
}

func updateStepper(view: CircleView) {
redStepper.value = 0.0
greenStepper.value = 0.0
blueStepper.value = 0.0

stepperChange(redStepper)
stepperChange(greenStepper)
stepperChange(blueStepper)
}

关于ios - swift 委托(delegate)不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35353882/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com