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;//
    }
  }
}
Steve
  • 1,121
  • 1
  • 12
  • 32
  • 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. – dfrib Feb 11 '16 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 – Hugo Alonso Feb 11 '16 at 22:52

2 Answers2

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!

Ataias Reis
  • 323
  • 1
  • 9
  • I had a problem with this line. the error said xc_bad_instruction. – Steve Feb 12 '16 at 02:21
  • circleViewDelegate!.delegate = self – Steve Feb 12 '16 at 02:22
  • 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 http://stackoverflow.com/questions/35319995/access-objects-in-viewcontroller-from-a-different-class-in-swift/35323453?noredirect=1#comment58398568_35323453 – Ataias Reis Feb 12 '16 at 08:49
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.

D. Greg
  • 991
  • 1
  • 9
  • 21