斯威夫特代表不工作

时间:2016-02-12 02:38:49

标签: ios swift uiview delegates viewcontroller

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

的ViewController:

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;
}

}

的UIView:

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()
        }
    }
}

2 个答案:

答案 0 :(得分:2)

试试这个:

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)
}

答案 1 :(得分:1)

而不是像circleViewDelegate!.delegate = self viewDidLoadcircleView1.delegate = self console.log($("#to-append > div").length); 那样分配代理人