无法识别的选择器发送到实例,UISlider swfit3

时间:2017-03-23 21:22:35

标签: ios swift uislider unrecognized-selector

我正在与UISlider合作,我想要认识到价值的变化:

var sliderGeofence: UISlider!

viewDidLoad()内:

 self.sliderGeofence = UISlider(frame: CGRect(x: 20, y: 20, width: self.view.frame.size.width - 50, height: 50))
        self.sliderGeofence?.maximumValue = 100000
        self.sliderGeofence?.minimumValue = 500
        self.sliderGeofence?.value = 100
        self.sliderGeofence?.isUserInteractionEnabled=true
        self.sliderGeofence?.addTarget(self, action: Selector("geofenceValueChange:"),for: UIControlEvents.valueChanged)
        gmsMap.addSubview(self.sliderGeofence!)
        sliderGeofence?.isHidden = true

我的价值变动方法:

 @IBAction func geofenceValueChange(sender: AnyObject)
    {
    print("entro")
    }

该应用程序会加载滑块,但是,当我更改其值时会发生这种情况:

  

***由于未捕获的异常终止应用' NSInvalidArgumentException',原因:' - [MocaAdmin.FirstViewController   geofenceValueChange:]:发送到实例的无法识别的选择器   0x7fca61511480'

我做错了什么?

2 个答案:

答案 0 :(得分:3)

将目标更改为以下内容:

sliderDemo.addTarget(self, action: #selector(geofenceValueChange(_:)), for: .valueChanged)

然后功能为:

func geofenceValueChange(_ sender:UISlider) {
    print("entro")
}

不需要函数中的@IBAction

答案 1 :(得分:2)

你应该像这样使用选择器:

self.sliderGeofence?.addTarget(self, action: #selector(FirstViewController.geofenceValueChange(_:)), for: .valueChanged)

这样,Xcode甚至会建议使用函数并自动完成您的输入,以确保工作正常。

希望有所帮助!