How to access the value of a UIStepper inside a Cocoa Touch Class

139 views Asked by At

I'm trying to create a custom slider, using a cocoa touch class, whose maximum value is determined by a UIStepper. I have the UIStepper wired to my view controller and I want to reference its value inside the cocoa touch class as the slider's maximum value. What is the syntax for referencing the stepper's value inside the class?

I keep getting the error use of unresolved identifier.

Here is my viewController:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var stepperValue: UIStepper!

    @IBOutlet weak var label: UILabel!

    let slider1 = Slider1(frame: CGRectZero)


    override func viewDidLoad() {
        super.viewDidLoad()

        label.textColor = UIColor.darkTextColor()
        slider1.backgroundColor = UIColor.lightGrayColor()
        view.addSubview(slider1)

    }

    override func viewDidLayoutSubviews() {
        let margin: CGFloat = 20.0
        let width = view.bounds.width - 2 * margin
        slider1.frame = CGRect(x: margin, y: 3 * margin, width: width, height: 1.5 * margin)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

Here is my UIControl subclass:

class Slider1: UIControl {

    var minimumValue = 0.0
    var maximumValue = stepperValue.value
    var value = 0

    let trackLayer = CALayer()
    var trackHeight:CGFloat = 2.0
    var trackColor = UIColor.blackColor().CGColor

    var tickHeight:CGFloat = 8.0
    var tickWidth: CGFloat = 2.0
    var tickColor = UIColor.blackColor().CGColor

    let thumbLayer = CALayer()
    var thumbColor = UIColor.blackColor().CGColor
    var thumbMargin:CGFloat = 2.0

    var thumbWidth: CGFloat {
        return CGFloat(bounds.height)
    }
}
1

There are 1 answers

0
Duncan C On

If you want a slider, use a slider. You will have a LOT of work to do if you want to create your own slider-like control starting from UIControl.

Just make your view controller drive everything.

Have the value changed event on your stepper trigger an action in the view controller.

In that stepperValueChanged action, fetch the new value of the stepper and use it to change the maximum value of the slider. That's all there is to it.