Search code examples
iosswiftuisliderunrecognized-selector

unrecognized selector sent to instance , UISlider swfit3


I'm working with UISlider and I want recognize a change of value:

var sliderGeofence: UISlider!

Inside my 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

My value-change method:

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

The app loads the slider, however, when I change its value this happens:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MocaAdmin.FirstViewController geofenceValueChange:]: unrecognized selector sent to instance 0x7fca61511480'

What am I doing wrong?


Solution

  • Change the target to the following:

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

    And then the function to:

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

    No need for the @IBAction in the function.