4

I have a stepper in my ViewController that controls the value of a variable in my UIView. When I reset the variables to 0 in my UIView, I also want the stepper and label to reset, but they won't. I tried using a delegate, but am getting an error. Thanks in advance!

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()
        }
    }
}
6
  • What is the error you get? Commented Feb 12, 2016 at 2:48
  • on this line: circleViewDelegate!.delegate = self , i get the error: EXC_BAD_INSTRUCTION Commented Feb 12, 2016 at 3:15
  • You don't need circleViewDelegate - you want to say circleView1.delegate=self Commented Feb 12, 2016 at 3:30
  • thanks i fixed it, but I'm still having the problem in my question Commented Feb 12, 2016 at 3:32
  • Set a breakpoint. Is your delegate method being called? Commented Feb 12, 2016 at 3:33

2 Answers 2

2

Try this:

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)
}
Sign up to request clarification or add additional context in comments.

1 Comment

I've updated the answer to call the IBAction method you were probably expecting to be called. This will update both the label and the CircleView.
1

Instead of assigning the delegate like this circleViewDelegate!.delegate = self in viewDidLoad try circleView1.delegate = self

1 Comment

it fixed the error but i still have the same problem in my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.