0

I added MTCircularSlider pod in my project. My viewController looks like below:

class ViewController: UIViewController {

    @IBOutlet var slider: MTCircularSlider!

    @IBOutlet var sliderLabel: UILabel!

    let temperatureArray = ["7.5","8.0","8.5","9.0","9.5","10.0","10.5","11.0","11.5","12.0","12.5","13.0","13.5","14.0","14.5","15.0","15.5","16.0","16.5","17.0","17.5","18.0","18.5","19.0","19.5","20.0","20.5","21.0","21.5","22.0","22.5","23.0","23.5","24.0","24.5","25.0","25.5","26.0","26.5","27.0","27.5","28.0","28.5"]

    override func viewDidLoad() {
        super.viewDidLoad()
        self.slider.addTarget(self, action: #selector(valueChange), for: .valueChanged)

    }

    @objc func valueChange() {
        //Slider value change
        let value = Float(slider.value)
        let numberString = String(value)
        let numberComponent = numberString.components(separatedBy :".")
        let integerNumber = Int(numberComponent [0])!
        let fractionalString = numberComponent[1].prefix(1)

        var fractionalNumber:Int = Int(fractionalString)!

        if fractionalNumber >= 5 {
            fractionalNumber = 5
        }
        else {
            fractionalNumber = 0
        }

        let temperature = String(integerNumber) + "." + String(fractionalNumber)
        if temperature == "7.5" {
            sliderLabel.text = NSLocalizedString("min", comment: "")
        }
        else if temperature == "28.5" {
            sliderLabel.text = NSLocalizedString("max", comment: "")
        }
        else {
            sliderLabel.text = temperature + " °C"
        }

        if temperatureArray.contains(temperature) {
            let index = temperatureArray.firstIndex(of: temperature)

            print(index!)
        }
    }
}

My UI like below:

enter image description here

My rootView is like below:

I am loading this viewController on TabBar with using below code:

class dashboardViewController: UIViewController {

    func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
switch item.tag {
        case 0:
            subTitleLabel.text = "Temperature"
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController                
            self.view.insertSubview(viewController.view!, belowSubview: self.dashboardTabBar)
}
}

When I load viewController normally means when push this View then it works fine. But when I add on tab click then my function valueChange() is not called. Please give me any solution to solve this.

Vikas
  • 1,548
  • 2
  • 12
  • 25
  • where do you create the UI? Is it storyboard or xib? Provide details for that as well. That would help us. – Rohan Bhale Apr 12 '19 at 06:20
  • How do you initialize and present an instance of the view controller? Is it through code or you are solely dependent on the storyboard? – Rohan Bhale Apr 12 '19 at 06:32
  • How does your view controller show its view on the device(or simulator)?. Do you instantiate the view controller your self or through segues? – Rohan Bhale Apr 12 '19 at 06:34
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/191701/discussion-between-rohan-bhale-and-vikas). – Rohan Bhale Apr 12 '19 at 06:43
  • I updated my question...I found problem, but not finding any solution. – Vikas Apr 12 '19 at 10:17
  • Can you try after the insertSubview call view.bringSubviewToFront(viewController!.slider) just to ensure that somehow the view hierarchy is not the causing the problem. In your example if you touch the slider is visually moving? – axel Apr 12 '19 at 10:30
  • In may case slider is moving...but not calling it's method – Vikas Apr 12 '19 at 10:32
  • same problem....nothing change – Vikas Apr 12 '19 at 10:35
  • hmm unfortunately I don't have any other idea. – axel Apr 12 '19 at 11:01

2 Answers2

1

Try this

slider.addTarget(self, action: #selector(ViewController.handle(_:)), for: . valueChanged)

    // Handle Action

        @objc func handle(_ sender: AnyObject) {


    }
karthikeyan
  • 3,821
  • 3
  • 22
  • 45
-1

As given in GitHub - MTCircularSlider reference, the ValueChange method has one argument. Probably sender argument. Please see their code below.

self.slider?.addTarget(self, action: Selector("valueChange:"), forControlEvents: .ValueChanged)

But your code seems to be missing that. Can you try something like this?

@objc
func valueChange(sender: AnyObject) {

}

Please let me know of this helps.

Anand
  • 1,820
  • 2
  • 18
  • 25
  • Are you sure IBOutlet is connected in storyboard and self.slider is not null in viewDidLoad? Please check that in debugger. – Anand Apr 12 '19 at 05:58
  • I updated my question...I found problem, but not finding any solution. – Vikas Apr 12 '19 at 10:14