0

I want my stepper values to reset when certain variables in a separate class equal each other. I tries using an instance of my ViewController but I'm getting errors. My variables reset, but the steppers do not.

CircleView Class:

class CircleView: UIView 
{  
var colors1=ViewController()
func updateStepper
  {
    if(redd1==Int(red1)&&greenn1==Int(green1)&&bluee1==Int(blue1))
    {
        redd1=0;
        greenn1=0;
        bluee1=0;
        colors1.redStepper.value=0.0;//
        colors1.greenStepper.value=0.0;//
        colors1.blueStepper.value=0.0;//
    }
  }
}

ViewController Class:

class ViewController: UIViewController 
{
var colors = CircleView()

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

@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;
}
}
3
  • colors1 is a new ViewController you made. It is not a reference to the existing ViewController that created the CircleView in the first place. Commented Feb 10, 2016 at 18:04
  • You should spend some time reading about "the delegate pattern" which is commonly used for inter-object communication. Commented Feb 10, 2016 at 18:04
  • As you have reference to your circleView1 in your viewController. Why don't you call updateStepper in your IBActions. Secondly what is the need of reference of ViewController in your circle class when you can perform functions regarding to your viewController in its own class. Commented Feb 10, 2016 at 18:08

1 Answer 1

2

You can use the delegate patterned like this:

// Add a delegate to your CircleView to reach back to your owning ViewController
protocol CircleViewDelegate {
    func updateRedStepperValue(value: Double)
    func updateGreenStepperValue(value: Double)
    func updateBlueStepperValue(value: Double)
}

class CircleView: UIView
{
    var colors1=ViewController()

    var delegate: CircleViewDelegate?

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

            // Call back to your delegate
            delegate?.updateBlueStepperValue(0.0)
            delegate?.updateGreenStepperValue(0.0)
            delegate?.updateRedStepperValue(0.0)
        }
    }
}

Your ViewController would then subscribe to the delegate call backs:

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!

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


    // Your delegate callback methods
    func updateRedStepperValue(value: Double) {
        redStepper.value = value
    }
    func updateGreenStepperValue(value: Double) {
        greenStepper.value = value
    }
    func updateBlueStepperValue(value: Double) {
        blueStepper.value = value
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        // Make sure to set the delegate outlet
        colors.delegate = self
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

i get this error "Cannot assign value of type 'ViewController' to type 'CircleViewDelegate?'"
there was a typo, fixed it.
i appreciate it but the error is still there after fixing the typo

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.