unrecognized selector sent to instance when i call from another class - swift

When i call a recognizer from another class this error appears:
unrecognized selector sent to instance,
if you call no error in the same class
var orderViewCard = OrderVC()
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(orderViewCard.handleCardTap(recognzier:)))
orderViewCard.handleArea.addGestureRecognizer(tapGestureRecognizer)
in OrderVC:
#objc
func handleCardTap(recognzier:UITapGestureRecognizer) {
switch recognzier.state {
case .ended:
animateTransitionIfNeeded(state: nextState, duration: 0.9)
default:
break
}
}

What you need to do is to select target accordingly
var orderViewCard = OrderVC()
let tapGestureRecognizer = UITapGestureRecognizer(target: orderViewCard, action: #selector(orderViewCard.handleCardTap))
orderViewCard.handleArea.addGestureRecognizer(tapGestureRecognizer)

Related

Swift: addGestureRecognizer for UIView class

I use this code:
class StarClass: UIView {
#IBOutlet weak var bgStar: UIView!
class func createMyClassView() -> StarClass {
let myClassNib = UINib(nibName: "Star", bundle: nil)
let nW = myClassNib.instantiate(withOwner: nil, options: nil)[0] as! StarClass
nW.bgStar.layer.cornerRadius = 15
nW.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleStar(sender:))))
return nW
}
#objc func handleStar(sender: UITapGestureRecognizer) {
print("iosLog STAR")
}
}
After run, if I clicked on the view, I get error below:
2018-07-10 11:11:50.496349+0430 Ma[23098:89853] +[Ma.StarClass
handleStarWithSender:]: unrecognized selector sent to class
0x10ef5cac0 2018-07-10 11:11:50.513392+0430 Ma[23098:89853] *
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '+[Ma.StarClass
handleStarWithSender:]: unrecognized selector sent to class
0x10ef5cac0'
* First throw call stack:...
There seems to be a problem with the line :
nW.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleStar(sender:))))
Here, inside a class function, self refers to the Class type, not its object. But you are adding the gesture recogniser on an object. The solution here would be to do this:
nW.addGestureRecognizer(UITapGestureRecognizer(target: nW, action: #selector(handleStar(sender:))))

Unrecognized selector error on swipe left gesture swift

I am attempting to create a simple swift left segue to the next view controller, however I am getting an unrecognized selector error. This the code failing:
override func viewDidLoad() {
super.viewDidLoad()
//below creates the instances for swiping to change screens
//only added right here because it's the main screen
var swipeLeft : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipe:"))
swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
self.view.addGestureRecognizer(swipeLeft)
func swipe(Sender: UISwipeGestureRecognizer!) {
print("swiped left")
let vc = self.storyboard!.instantiateViewControllerWithIdentifier("OnDeck") as! OnDeck
self.presentViewController(vc, animated: true, completion: nil)
}
}
This is the error I am getting:
2017-06-07 19:07:00.990 Test[65722:3911717] -[Test.GameView swipe:]: unrecognized selector sent to instance 0x7fc2d310f700
2017-06-07 19:07:01.000 Test[65722:3911717] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Test.GameView swipe:]: unrecognized selector sent to instance 0x7fc2d310f700'
In your case for Swift 3 the selector should look like this:
#selector(swipe(Sender:))
You can use the #selector notation to get compile-time checks.
For Swift 2 the selector would be:
#selector(swipe(_:))
You need to try it like that :
let swiftLeft : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(self.swipe(Sender:)))
swiftLeft.direction = .left
self.view.addGestureRecognizer(swiftLeft)
func swipe(Sender: UISwipeGestureRecognizer!) {
print("swiped left")
}
If you are working on Swift 2, you need to add the selector like that
selector(self.swipe(_:))

selector with argument gestureRecognizer

Argument of '#selector' does not refer to an '#objc' method, property,
or initializer
Problem : Above Error when i try to pass argument with selector
code snippet:
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(labelPressed(i: 1)))
func labelPressed(i: Int){
print(i)
}
You cannot pass a parameter to a function like that. Actions - which this is, only pass the sender, which in this case is the gesture recognizer. What you want to do is get the UIView you attached the gesture to:
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(labelPressed())
func labelPressed(_ recognizer:UITapGestureRecognizer){
let viewTapped = recognizer.view
}
A few more notes:
(1) You may only attach a single view to a recognizer.
(2) You might want to use both the `tag` property along with the `hitTest()` method to know which subview was hit. For example:
let view1 = UIView()
let view2 = UIView()
// add code to place things, probably using auto layout
view1.tag = 1
view2.tag = 2
mainView.addSubview(view1)
mainView.addSubview(view2)
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(mainViewTapped())
func mainViewTapped(_ recognizer:UITapGestureRecognizer){
// get the CGPoint of the tap
let p = recognizer.location(in: self)
let viewTapped:UIView!
// there are many (better) ways to do this, but this works
for view in self.subviews as [UIView] {
if view.layer.hitTest(p) != nil {
viewTapped = view
}
}
// if viewTapped != nil, you have your subview
}
You just should declare function like this:
#objc func labelPressed(i: Int){ print(i) }
Update for Swift 3:
Using more modern syntax, you could declare your function like this:
#objc func labelTicked(withSender sender: AnyObject) {
and initialize your gesture recognizer like this, using #selector:
UITapGestureRecognizer(target: self, action: #selector(labelTicked(withSender:)))

Check which image view was pressed using UITapGestureRecognizer in Swift

I have three UITapGestureRecognizers.
They look like that:
gestureImageViewUp = UITapGestureRecognizer(target: self, action: #selector(ViewController.checkChoice(_:)))
self.imageViewUp.addGestureRecognizer(gestureImageViewUp)
gestureImageViewDown = UITapGestureRecognizer(target: self, action: #selector(ViewController.checkChoice(_:)))
self.imageViewDown.addGestureRecognizer(gestureImageViewDown)
gestureImageViewMiddle = UITapGestureRecognizer(target: self, action: #selector(ViewController.checkChoice(_:)))
self.imageViewMiddle.addGestureRecognizer(gestureImageViewMiddle)
I want to check which of them was pressed. How can I solve that?
You shouldn't need more then one recogniser, just attach it to the view and in the selector check which imageview was clicked.
func onPress(_ guesture: UIGestureRecognizer) {
guard let location = guesture.location(in: self.view) else { return }
if gestureImageViewUp.frame.contains(location) {
// …
}
if gestureImageViewDown.frame.contains(location) {
// …
}
if gestureImageViewMiddle.frame.contains(location) {
// …
}
}
Not tested sorry, it would be easier if you pasted code instead of snapshots

How to make a gestureRecogniser selector work in swift 3?

Below is the code I have been using to try and add a gesture recogniser to something. I am getting the yellow error: "No method declared with objective C selector dragging" and then the program is crashing when I go to pan on it. The code and way of using a selector seems to work in all tutorials but it's the problem here.
class GameViewController: UIViewController, UIGestureRecognizerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let p = UIPanGestureRecognizer(target: self, action: #selector("dragging"))
p.delegate = self
characterGridView!.addGestureRecognizer(p)
}
func dragging(p: UIPanGestureRecognizer) {
print("works")
}
Your selector is incorrect.
Change
let p = UIPanGestureRecognizer(target: self, action: #selector("dragging"))
to
let p = UIPanGestureRecognizer(target: self, action: #selector(dragging(p:)))