如何在uiControl类中访问appdelegate值

时间:2017-08-03 06:18:13

标签: swift rangeslider

我想在sliderView中显示文本字段更改。即,当用户更改文本字段中的值时,应更新滑块值。我正在使用带有两个拇指的RangeSlider。滑块值是基于webservice调用设置的。我将响应存储在appDelegate类中,我正在获取它。但问题是在UIControl类中我在beginTracking和EndTracking中获取appdelgate值。仅当我在幻灯片(拖动)中进行一些更改时才会调用这些函数。但是在更新文本字段时,只调用初始化部分。如何在init函数中从appdelegate获取值?

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()
    }
}

1 个答案:

答案 0 :(得分:0)

您可以像这样访问appDelegate实例

let appDelegate =  UIApplication.shared.delegate as? AppDelegate
let someValue = appDelegate?.someVariableInAppDelegate