Unrecognized selector error on swipe left gesture swift - 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(_:))

Related

unrecognized selector sent to instance when i call from another class

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)

How I can use performSegue in swift 3.0?

#IBOutlet weak var viewHistory: UIView!
#IBOutlet weak var viewGoal: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let tapHistory = UITapGestureRecognizer(target: self, action: #selector(goHistory))
tapHistory.numberOfTapsRequired = 1
tapHistory.numberOfTouchesRequired = 1
tapHistory.isEnabled = true
viewHistory.addGestureRecognizer(tapHistory)
// Do any additional setup after loading the view.
}
#objc func goHistory(){
self.performSegue(withIdentifier: "Segue_History", sender: self)
}
In StoryBoard A, I set the Triggered Segues to MyPage->History.
So I set the segue identifier "Segue_History"
When I tap the view in MyPage, goHistory function call performSegue.
But it occurs error like this.
2018-11-02 16:43:45.875605+0900
testApp[1729:436205]
-[testApp.VC_History tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x106a83d40
2018-11-02 16:43:45.891627+0900
testApp[1729:436205] *** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[testApp.VC_History tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x106a83d40'
I think I set the identifier at segue, but it is not.
Do you have any idea of it?

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:))))

Error in Swift when using UIImageView with selector #objc C function when reading sender.tag

An image sends an action to an #objc C function when pressed:
let imageView = UIImageView()
imageView.tag = 3
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(ItemAction)))
imageView.image = UIImage(named: (itemDictionary[character.Equipment[imageView.tag]]!.image))
overlayView.addSubview(imageView)
this is the function that should be called:
#objc func ItemAction(sender: UIImageView!) {
print(sender.tag)
print("Item pressed from sender ")
}
the function runs, but when it comes to printing the sender tag I get an error message and the program quits:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITapGestureRecognizer tag]:
unrecognized selector sent to instance 0x600002b42a00'
The whole setup with a UIButton works.
How can I read the tag of an UIImageView in an external function?
here you go
#objc func itemAction(_ sender: UITapGestureRecognizer){
if let tag = sender.view?.tag{
print("ImageView tag \(tag)")
}
}
in your function change your method signature to
#objc func ItemAction(_ sender: UITapGestureRecognizer)

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:)))