-2

I want to display the textfield changes in sliderView. I.e., when a user changes the value in the textfield the slider value should be updated. I'm using a RangeSlider with two thumbs.The slider value is set based on a webservice call. I'm storing the response in an appDelegate class and I'm fetching it. But the problem is in the UIControl class I'm fetching appdelgate values in beginTracking and EndTracking. These functions will be called only when I do some changes in slide(dragging). But on updating the textfield only the initialization part is called. How to fetch values from the appdelegate in init function?

class RangeSlider: UIControl {
    var minimumValue = 0.0
    var maximumValue = 100.0
    var lowerValue = 0.0
    var upperValue = 100.0

    let trackLayer = CALayer()
    let lowerThumbLayer = RangeSliderThumbLayer()
    let upperThumbLayer = RangeSliderThumbLayer()

    var previousLocation = CGPoint()

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

    override init(frame: CGRect) {
        super.init(frame: frame)

        trackLayer.backgroundColor = UIColor.yellow.cgColor
        layer.addSublayer(trackLayer)

        lowerThumbLayer.backgroundColor = UIColor.black.cgColor
        layer.addSublayer(lowerThumbLayer)

        upperThumbLayer.backgroundColor = UIColor.black.cgColor
        layer.addSublayer(upperThumbLayer)

        lowerThumbLayer.rangeSlider = self
        upperThumbLayer.rangeSlider = self

        updateLayerFrames()
    }
}
jotasi
  • 5,077
  • 2
  • 29
  • 51
Shwetha
  • 5
  • 7

1 Answers1

0

You can access the appDelegate instance like this

let appDelegate =  UIApplication.shared.delegate as? AppDelegate
let someValue = appDelegate?.someVariableInAppDelegate
Mohammad Sadiq
  • 5,070
  • 28
  • 29
  • Im able to access that inside begin tracking and end tracking.... But when I call it in init method NAN error will come....why is it so? – Shwetha Aug 03 '17 at 06:50
  • Need to make sure that the values are available at the time you are trying to access them. Might be first time these values would not be available. Try initializing the values to 0.0 . – Mohammad Sadiq Aug 03 '17 at 07:10
  • Have initialized values in appDelegate and UIControl class..var minimumValue = 0.0 var maximumValue = 0.0 var lowerValue = 0.0 var upperValue = 0.0........Same error is coming – Shwetha Aug 03 '17 at 07:21
  • I have an expression to calculate lowerThumbLayer.frame i.e, lowerThumbLayer.frame= CGRect(x: lowerThumbCenter - thumbWidth / 2.0, y: 0.0, width: thumbWidth, height: thumbWidth).Here it is giving NAN.Since I have initialized the values as 0,when i try to divide 0 by some value NAN error is coming. – Shwetha Aug 03 '17 at 07:23
  • dont divide by zero. put if condition and avoid it – Mohammad Sadiq Aug 03 '17 at 07:33
  • sorry...not dividing by zero....the lowerthumbcenter is coming as zero....lower thumbcenter value depends on upper and lower value in my code – Shwetha Aug 03 '17 at 07:53
  • let lowerThumbCenter = CGFloat(positionForValue(value: lowerValue)) lowerThumbLayer.frame = CGRect(x: lowerThumbCenter - thumbWidth / 2.0, y: 0.0, width: thumbWidth, height: thumbWidth) – Shwetha Aug 03 '17 at 07:56