swiftuisegmentedcontroluitapgesturerecognizeraddtarget

UITapGestureRecogniser add target not working


i have a objc function:

@objc private func segmentedControlViewTapped(gestureRecognizer: UITapGestureRecognizer)

I am trying to add this as a target for a gestureRecognizer but the code keeps crashing when i tap the segment control.

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(SegmentedControl.segmentedControlViewTapped(gestureRecognizer:)))

crashes due to: unrecognized selector sent


Solution

  • The action needs to point to a function in the target you specified.

    Assuming the segmentedControlViewTapped is in your view or controller, change your code like this:

    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(segmentedControlViewTapped(gestureRecognizer:)))