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

Posted

技术标签:

【中文标题】UISwipeGestureRecognizer 抛出无法识别的选择器发送错误【英文标题】:UISwipeGestureRecognizer throw unrecognised selector sent error 【发布时间】:2016-09-20 05:44:51 【问题描述】:

我的 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
        //
    

【问题讨论】:

斯威夫特版本? Swift 3.0 版 @Vyshakh - 检查更新的答案 【参考方案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:)))

注意: 一个建议是batter 方法名称总是以小写而不是大写开头。所以如果你使用handleSwipes而不是HandleSwipes,那就是击球手。

【讨论】:

【参考方案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")
    

【讨论】:

以上是关于UISwipeGestureRecognizer 抛出无法识别的选择器发送错误的主要内容,如果未能解决你的问题,请参考以下文章

识别用户剪切/滑动手势 (UISwipeGestureRecognizer)

UISwipeGestureRecognizer 滑动长度

UISwipeGestureRecognizer 只有一个方向工作

如何检测 UISwipeGestureRecognizer 的结束?

如何使 UISwipeGestureRecognizer 在 UIScrollView 中工作

UISwipeGestureRecognizer 没有触发