0

我在我的项目中添加了 MTCircularSlider 吊舱。我的视图控制器如下所示:

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

我的用户界面如下:

在此处输入图像描述

我的 rootView 如下所示:

我正在使用以下代码在 TabBar 上加载此 viewController:

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

当我加载 viewController 通常意味着当推送此视图时它可以正常工作。但是当我在选项卡上单击时,我的函数 valueChange() 不会被调用。请给我任何解决方案来解决这个问题。

4

2 回答 2

1

尝试这个

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

    // Handle Action

        @objc func handle(_ sender: AnyObject) {


    }
于 2019-04-12T06:08:28.817 回答
-1

正如GitHub - MTCircularSlider参考中给出的,ValueChange 方法有一个参数。可能是发件人的论点。请在下面查看他们的代码。

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

但是您的代码似乎缺少这一点。你可以试试这样的东西吗?

@objc
func valueChange(sender: AnyObject) {

}

请让我知道这有帮助。

于 2019-04-12T05:50:58.493 回答