0

I need the stepper and label to reset back to 0 at the same time that my variables reset. The problem is the steppers and labels are in a different class and are not resetting when the variables do. I tried using delegates(if someone can show me the best way that would be great) instead of an instance of my view controller, but I can't get anything to work. Thanks for any help in advance.

ViewController:

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

UIView:

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;//
    }
  }
}
2
  • Look for questions/information regarding "delegates" here on SO (or the web); this is a classical scenario where you'd want to make use of delegates to do callbacks from the UIView sub class to your view controller. Commented Feb 11, 2016 at 21:33
  • Your code doesn't work for several reasons, but for a better answer I will need you to paste your exact code, CircleView and ViewController are missing some vars Commented Feb 11, 2016 at 22:52

2 Answers 2

2

I do not quite understand your code, like the "if" condition in your CircleView, the lack of parameters to the method "updateStepper". I am assuming you just wrote some "swift-pseucode" and I will ignore some parts of it to explain how you could implement a delegate for it. Below is an example code:

import UIKit

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


class ViewController: UIViewController, CircleViewDelegate{

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

    var circleViewDelegate: CircleView!

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

    func updateStepper(view: CircleView) {
        //code you want to execute when you call updateStepper() in the CircleView()
    }

}

class CircleView: UIView {

    weak var delegate: CircleViewDelegate?

    func updateStepper() {
        //whenever you want your viewController to updated other views based
        //on a condition inside an element like UIView, you can use a delegate
        //this way, your code is executed by the ViewController whenever you want
        delegate?.updateStepper(self)
    }

}

A callback in your UIView must be set to call "updateStepper" when you want. Unfortunately, I didn't quite understand the time it should be called according to your question.

I hope this helps!

Sign up to request clarification or add additional context in comments.

3 Comments

I had a problem with this line. the error said xc_bad_instruction.
circleViewDelegate!.delegate = self
I think I forgot one line... Try "circleViewDelegate = circleView1" just before setting the delegate to self. Let me know what happens. Today I am not with a computer to test it, bu you can compare it with stackoverflow.com/questions/35319995/…
1

Have you tried NSNotification?

If it's always going to reset to zero, then create a func without the if statement in CircleView:

func resetStepper(not: NSNotification) {
  r1 = 0
  g1 = 0
  b1 = 0
  c1.rStep.value = 0.0
  c1.bStep.value = 0.0
  c1.gStep.value = 0.0
}

Also in CircleView's createView func, add:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "resetStepper:", name: "ResetStepper", object: nil)

Then in the view controller, post a notification from whichever button is calling it.

@IBAction func callReset(sender: AnyObject) {
  NSNotificationCenter.defaultCenter().postNotificationName("ResetStepper", anObject: nil)
}

That will send the notification that CircleView is listening for to call the function.

Hope that works for you.

2 Comments

There are better ways to achieve this, NSNotification should be used in exceptional cases
Can you provide a better way? I'd like to learn. Also, what exceptional cases would NSNotification be used?

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.