Vyshakh Vyshakh - 2 months ago 24
Swift Question

UISwipeGestureRecognizer throw unrecognised selector sent error

I am getting

unrecognized selector sent
error in my iOS application. I tried solving the issue based on the answers mentioned in other similar thread but failed. Please have a look at the code below and help me with this.

Thanks

class ThirdViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.



let leftSwipe = UISwipeGestureRecognizer(target: self, action: Selector(("HandleSwipes:")))

let rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector(("HandleSwipes:")))

leftSwipe.direction = .left
rightSwipe.direction = .right


view.addGestureRecognizer(leftSwipe)
view.addGestureRecognizer(rightSwipe)



}



func HandleSwipes(sender: UISwipeGestureRecognizer) {

//if(sender.direction == .left)
//{
// tabBarController?.selectedIndex = 1
//}
}
}

Answer

Write selector like this.

Swift 2.3 or lower.

let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(HandleSwipes(_:)))
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(HandleSwipes(_:)))

Swift 3

let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(HandleSwipes(sender:)))
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(HandleSwipes(sender:)))

Note: One suggestion it is batter method name always start with lower case not upper case. So it is batter if you use handleSwipes instead of HandleSwipes.