Search code examples
iosswiftuislider

How to call a Button from another view with Swift


I'm using this repo but it doesn't have any function to alert me when the slider changes its value so I tried to do it myself.

First I tried to do it with the slider's action but I want only the value when the user will stop sliding which is an Int(the slider's action returned me a lot of values such as 1.2,1.3,1.4 etc).

I found a way to take the value when the user will stop sliding from a function that's inside this repo like this:

    internal func didMoveSliderStepValue(sendValueChangedEvent: Bool = false) {
            let intValue = Int(round(self.value))
            let floatValue = Float(intValue)

            print(intValue) //It prints only when the user stops sliding
            //a function to call after taking the value
            callTheButton()

            UIView.animateWithDuration(0.35, animations: {
                self.setValue(floatValue, animated: true)
            }) { (fin) in
                self.setThumbForSliderValue(floatValue)
                if sendValueChangedEvent {
                    self.sendActionsForControlEvents(.ValueChanged)
                }
            }
        }

func callTheButton(){
     buttonFromMainView()
}

Now I want to trigger a button (buttonFromMainView) that I have in my main view.

So my question is this:

Can I call the button from the swift file that the repo has? Or can I take only the value when the user ends sliding from UISlider functions?

Thanks in advance.


Solution

  • If you want easily access value after user finish touch you can set (for example in viewDidLoad)

    isContinous 
    

    property of your G8SliderStep (and any UISlider) and then implement valueChanged function like for any other UISlider

    @IBAction func valueChanged (sender: G8SliderStep)
    {
        // Your value will be like 2.3325 so you want to round it
        let newValue = round(sender.value)
    }