ios - 将对象传递给选择器

标签 ios swift uigesturerecognizer

我使用此代码来检测长按的元素:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// some stuff 

if let labelCell = model as? TTTLabelCell{
labelCell.delegate = self
labelCell.textLabel.addGestureRecognizer(longPressRec)
  }
}

这是手势识别器

longPressRec.addTarget(self, action:#selector(labelLongPressed))

这是选择器

   func labelLongPressed(){
        print("Label Long pressed")
   //  labelCell.backgroundColor = UIColor.blue
    }

正如代码中所注释的那样,我想要实现的是将选定的对象(labelCell)传递给选择器 labelLongPressed(),这将允许我更改一些属性。您知道如何做到这一点吗?

最佳答案

更改功能如下:

func labelLongPressed(_ sender: UITapGestureRecognizer){
        print("Label Long pressed")
        let labelCell: TTTLabelCell = sender.view // track as per your view hierarchy
         labelCell.backgroundColor = UIColor.blue
    }

关于ios - 将对象传递给选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43494205/

相关文章:

ios - 强制 Wifi 弹出窗口 : Click a link to open Safari

Swift Type<Type> 含义

swift - 在没有 var/let 的情况下在 Swift 中初始化变量

ios - 点击时如何对子类 UIView 执行操作?

ios - UIScrollView 在进入 UIPageViewController 时不滚动

ios - 需要 Google 移动广告 SDK 常量的值

javascript - 在设备从纵向/横向旋转时使用 javascript 或 CSS 进行重定向

ios - 如何防止 UITableView 滚动干扰我的手势识别器?

ios - HomeKit - 错误域=NSCocoaErrorDomain 代码=4097

objective-c - 如何在 Swift 中调用 Objective C 类方法