一尘不染

UISwipeGestureRecognizer引发无法识别的选择器发送错误

swift

我得到unrecognized selector sent我的iOS应用程序错误。我尝试根据其他类似主题中提到的答案解决问题,但失败了。请查看下面的代码,并为此提供帮助。

谢谢

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
        //}
    }
}

阅读 275

收藏
2020-07-07

共1个答案

一尘不染

这样写选择器。

Swift 2.3或更低版本。

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

迅捷3

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

注意: 一种建议是面糊方法名称始终以小写而不是大写开头。因此,如果您使用handleSwipes而不是,那就更糟了HandleSwipes

2020-07-07