无法识别的UITapGestureRecognizerselect器发送到实例
我已经寻找解决这个问题的方法,但是找不到解决这个问题的东西。 我从UITapGestureRecognizer获取上述exception。
这是简化的代码:
import UIKit; class ViewController : UIViewController, UIScrollViewDelegate { @IBOutlet weak var scrollView:UIScrollView!; var imageView:UIImageView!; override func viewDidLoad() { super.viewDidLoad(); ... set up imageView/scrollView here ... let doubleTapRecognizer = UITapGestureRecognizer(target: self, action: "onScrollViewDoubleTapped"); doubleTapRecognizer.numberOfTapsRequired = 2; doubleTapRecognizer.numberOfTouchesRequired = 1; scrollView.addGestureRecognizer(doubleTapRecognizer); } func onScrollViewDoubleTapped(recognizer:UITapGestureRecognizer) { } }
任何人都可以告诉这个代码有什么问题吗? 这对我来说似乎是正确的。 我怀疑它与分配ViewController作为委托scrollView(或反之亦然)? 然而,ViewController被设置为scrollView的委托。 但是,也许这是导致这个错误的其他东西?
尝试添加一个冒号到你的select器string。
let doubleTapRecognizer = UITapGestureRecognizer(target: self, action: "onScrollViewDoubleTapped:");
正如cabellicar123所提到的,这表明select器需要一个参数。
另外尝试添加一个参数到你的方法:
...(target: self, action: "yourMethodName:") func yourMethodName(sender: AnyObject?) { println("Clicked yourMethodName") }
Swift 4使用#selector
。
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didSelectItem(sender:))) @objc func didSelectItem(sender: AnyObject?) { print("didSelectItem: \(sender)") }
也许可以帮助别人:我有这个错误,因为我宣布私人的select器方法:
func setup() { let singleFingerTap = UITapGestureRecognizer(target: self, action: "didTapOnViewInteraction:") singleFingerTap.numberOfTapsRequired = 1 self.viewInteraction.addGestureRecognizer(singleFingerTap) } private func didTapOnViewInteraction(recognizer: UITapGestureRecognizer) { }
删除“私人”的关键字,一切都很好!
为迅速4
override func viewDidLoad() { super.viewDidLoad() let tap = UITapGestureRecognizer(target: self, action: #selector(SignUpViewController.click)) userImage.addGestureRecognizer(tap) userImage.isUserInteractionEnabled = true } @objc func click() { print("Tapped on Image") }
SignUpViewController是您的viewcontroller名称