Pass an additional argument for UItapgestureRecognizer with selector

综合技术 Hello, buddy! (源链)

I have two labels, Label1 and Label2. I want to make a single function that prints out which label is tapped by creating UITTapRecognizer for both labels calling the same function with selector that passes an argument. Below is the long way of doing it which is messy but works. If I know how to pass an argument (Int) into the selector, it would be alot cleaner.

let topCommentLbl1Tap = UITapGestureRecognizer(target: self, action: #selector(DiscoverCell().doubleTapTopComment1))
    topCommentLbl1Tap.numberOfTapsRequired = 2
    topCommentLbl1.userInteractionEnabled = true

let topCommentLbl2Tap = UITapGestureRecognizer(target: self, action: #selector(DiscoverCell().doubleTapTopComment2))
        topCommentLbl2Tap.numberOfTapsRequired = 2
        topCommentLbl2.userInteractionEnabled = true

func doubleTapTopComment1() {
    print("Double Tapped Top Comment 1")
func doubleTapTopComment2() {
    print("Double Tapped Top Comment 2")

Is there a way to modify the selector method such that I can do something like

func doubleTapTopComment(label:Int) {
    if label == 1 {
        print("label (label) double tapped")

Short answer: no

The selector is called by the UITapGestureRecognizer
, and you have no influence on what parameters it passes.

However, what you can do is query the recognizer’s view
property to get the same information.

func doubleTapComment(recognizer: UIGestureRecognizer) {
    if recognizer.view == label1 {
    else if recognizer.view == label2 {


【iOS 开发】使用 Jenkins + Fastlane 持续集成 iOS 项目... 持续集成( Continuous Integration 简称 CI )。 Jenkins 是一个开源项目,提供了一种易于使用的持续集成系统,使开发者从繁杂的集成中解脱出来,专注于更为重要的业务逻辑实现上。具体就不多说了,想要更多的了解可以去查一下,下面是集成方法。 ...
2018 iOS 面试题 必看20题 Apple Developer 前言 在 iOS 开发中,语言的选择是最初的一步。 Objective-C 是苹果为 iOS 和 Mac 开发量身定制的语言。 它随着 iPhone 的出现而大火,直到今天国内外大多数的 App 依然是用 Objective-C ...
Popover with integrated navigation controller does... I have a UIPopoverController hosting a UINavigationController, which contains a small hierarchy of view controllers. I followed the docs and for eac...
H264,H265 Encoder h265 : —————————————————————————————————————- 源代码地址: H264,H265Encode 博客地址:H264,H265Encode 简书地址: H264,H265Encode ——————————...
实现一个 JavaScriptCore 的 debugger —— iOS 篇... JSC 的 debugger 是个神奇的东西,在网上资料甚少,几乎完全搜索不到,尤其是 iOS 上,OC 接口没有暴露任何 debugger 信息。 不过好在 JSC 是开源的,通过分析源代码可以找到 JSC::Debugger 这个抽象类,我们继承这个抽象类,然后实现掉虚函数,创建实例并且把它...
Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » Pass an additional argument for UItapgestureRecognizer with selector

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录