我的iOS应用程序出现unrecognized selector sent
错误。我尝试根据其他类似线程中提到的答案解决问题,但失败了。请看下面的代码,并帮助我。
由于
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
//}
}
}
答案 0 :(得分:5)
像这样写选择器。
Swift 2.3或更低。
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:)))
注意:一个建议是电池方法名称始终以小写字母而不是大写字母开头。因此,如果您使用handleSwipes
而不是HandleSwipes
,那就更好了。
答案 1 :(得分:2)
你需要像
一样打电话 override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
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)
{
print("Swiped left")
}
}