Swift 3: Unrecognized selector sent to instance Xcode 8
Solution 1
If you are using swift
3 your selector
should be like this
#selector(self.handleTap(recognizer:))
Solution 2
You should write this statement in different way. use following line
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(ViewController.handleTap(_:)))
instead of
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: Selector(("handleTap:")))
New Way
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(YourViewController.handleTap(_:)))
Solution 3
class ViewController: UIViewController {
let customView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handleTap(_:)))
customView.addGestureRecognizer(panGestureRecognizer)
}
func handleTap(panGesture: UIPanGestureRecognizer) {
}
}
Don't forget to add your custom UIView to the VC.
Raul
Updated on September 06, 2020Comments
-
Raul over 3 years
I have programmatically created an
UIView
and addedUIPanGestureRecognizer
to it:class ViewController: UIViewController{ var preludeView: UIView! override func viewDidLoad() { super.viewDidLoad() initViews() createConstrants() let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: Selector(("handleTap:"))) preludeView.addGestureRecognizer(panGestureRecognizer) } func handleTap(recognizer: UIPanGestureRecognizer) { print("WORKING!!!!") } func initViews() { ... } func createConstrants() { ... } }
But when I am touching the view Xcode is throwing an error:
2016-07-13 09:24:29.918 Draft_Hypa_02[661:83024] -[Draft_Hypa_02.ViewController handleTap:]: unrecognized selector sent to instance 0x17d94a10 2016-07-13 09:24:29.921 Draft_Hypa_02[661:83024] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Draft_Hypa_02.ViewController handleTap:]: unrecognized selector sent to instance 0x17d94a10' * First throw call stack: (0x249cf91b 0x2416ae17 0x249d52b5 0x249d2ee1 0x248fe238 0x294ae9eb 0x290e984f 0x28f7aff1 0x294afd4f 0x28f3ba57 0x28f38017 0x28f78ec9 0x28f7867b 0x28f49125 0x28f476d3 0x24991dff 0x249919ed 0x2498fd5b 0x248df229 0x248df015 0x25ecfac9 0x28fb1189 0x93144 0x24587873) libc++abi.dylib: terminating with uncaught exception of type NSException
However, if I remove argument in the
handleTap
function and remove the colon in theSelector(("handleTap:"))
, everything works fine!Already spent a day to try to fix this issue and would be very appreciate for your help!
-
Raul almost 8 yearsXcode is throwing "Value of type 'ViewController' has no member 'handleTap'"
-
Raul almost 8 yearsXcode 8 is throwing "Value of type 'ViewController' has no member 'handleTap'"
-
Raul almost 8 yearsI am adding UIView in initViews() and put this function in the viewDidLoad()
-
Leo Dabus almost 8 years@Forze you need also to add an underscore before the method first parameter.
func handleTap(_ recognizer: UIPanGestureRecognizer) {
-
Raul almost 8 years@Nirav thanks, It works! Do you know in Swift 3 we must set external name for action function?
-
Nirav D almost 8 yearsYes in swift 3 to call the first parameter you must write parameter name, you can check this for more info
-
Mahendra Y almost 8 yearsuse self instead of ViewController or name of your viewController
-
Mahendra Y over 7 years@Forze: handleTap is name of method, You are calling func handleTap(sender: UIPanGestureRecognizer){}
-
Markinson over 7 years@Mahendre I used self and its still giving the same error
-
Mahendra Y over 7 years@Markinson Can you post your function and its calling syntax
-
Markinson over 7 years@Mahendre Y I figured it out. Your code was right. I just forgot to write the name of my .swift file before calling the func
-
Luda over 7 years@LeoDabus, you comment helped me a lot!