【问题标题】:iOS - How to pass Touch Event to Superview?iOS - 如何将触摸事件传递给 Superview?
【发布时间】:2019-08-09 08:02:24
【问题描述】:

我已经在我的 viewController 上实现了一个不可滚动的 tableView。在我的 viewController 上,我设置了 touchesBegan: 功能。

我希望它在我触摸我的 tableView 时触发,但我仍然不知道该怎么做。

知道我该如何做到这一点吗?

【问题讨论】:

  • 如何使用func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 来实现在单元格点击时触发的操作?

标签: ios objective-c cocoa-touch uiview


【解决方案1】:

我找到了两个解决方案:

  1. 如果您的 tableView 不需要处理触摸事件,您可以在 storyboard 中取消选中复选框 'User Interaction Enabled',然后 tableView 超级视图将处理 touchEvent。
  2. 如果您需要在单元格中处理事件,您可以在 UITableViewCell 子类中编写这样的代码,搜索下一个响应者,直到找到 ViewController:
class CustomCell: UITableViewCell {
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)

        var responder: UIResponder? = self
        var vc: UIViewController?

        while vc == nil {
            if responder is UIViewController {
                vc = responder as? UIViewController
                break
            }
            responder = responder?.next
        }

        vc?.touchesBegan(touches, with: event)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    相关资源
    最近更新 更多