HayashiEsme
HayashiEsme

Reputation: 333

UISlider not updating values

apologies if this is a stupid question. I can't seem to get my slider to update its value as its being interacted with. (I'm going to point everyone to the very last method in this long code)

class CustomSlider: UISlider {

    override func trackRect(forBounds bounds: CGRect) -> CGRect {
        var rect = super.trackRect(forBounds: bounds)
        rect.size.height = 7
        return rect
    }
}

class FactionButton: CustomSlider {
    var factionSlider = CustomSlider(frame: CGRect(x: 15, y: 542, width: 386, height: 57))

    func factionBalanceSlider(){
        factionSlider.minimumValueImage = #imageLiteral(resourceName: "Alliance Slider")
        factionSlider.maximumValueImage = #imageLiteral(resourceName: "Horde Slider")
        factionSlider.setThumbImage(#imageLiteral(resourceName: "Thumb Image"), for: .normal)
        factionSlider.minimumTrackTintColor = UIColor(red:0.08, green:0.33, blue:0.69, alpha:0.8)
        factionSlider.maximumTrackTintColor = UIColor(red:1.00, green:0.00, blue:0.00, alpha:0.59)

        factionSlider.setValue(0.5, animated: true)
        factionSlider.isContinuous = false

        factionSlider.addTarget(self, action: #selector(recordFactionBalance(sender:)), for: .valueChanged)
    }

    func getSlider() -> CustomSlider {
        return factionSlider
    }

    override func trackRect(forBounds bounds: CGRect) -> CGRect {
        var customBounds = super.trackRect(forBounds: bounds)
        customBounds.size.height = 10
        return customBounds
    }

    @objc func recordFactionBalance(sender: CustomSlider){
        //also calculates balance and adds it into the quiz data
        print("hi")
        print(sender.value) //It's this part that doesn't work 

    }

}

It's this bit nearest to the bottom that has the issue. (Everything else is fine) The action function doesn't seem to be triggered at all, even when I'm interacting with it. Neither print statements are being executed. Any ideas why?

Cheers

Upvotes: 0

Views: 247

Answers (1)

Kamran
Kamran

Reputation: 15238

From the getSlider(), i can guess you are using this class as a utility to get the CustomSlider. So, i suspect you are adding the slider to the view as below,

let container = FactionButton()
container.factionBalanceSlider()
let slider = container.getSlider()

self.view.addSubview(slider)

If you will not add the container to the view which is set as the receiver for .valueChange event so it will not get any event. To receive events you also need to add the container in the view as below,

self.view.addSubview(container)

Upvotes: 0

Related Questions