首页 > 解决方案 > 如何在 TableView 单元格中按住按钮时进行 segue

问题描述

我有一个非常复杂的问题,按钮已经有一个作为@objc 函数的点击动作。我可能必须为 cellForRow 函数实现代码,但它如何仅在这些函数起作用时调用?

@objc func longPress(gesture: UILongPressGestureRecognizer) {
        if gesture.state == UIGestureRecognizer.State.began {
            print("Long Press")
            //Was trying to figure out how to add segue call here 
        }
    }

    func addLongPressGesture(){
        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPress(gesture:)))
        longPress.minimumPressDuration = 0.75
        self.expandButton.addGestureRecognizer(longPress)
    }

标签: iosswiftuitableviewbuttonsegue

解决方案


您可以在情节提要中从表格所在的 ViewController 中添加一个 segue 到您想要转到的 View 中。然后给它一个标识符并调用func performSegue(withIdentifier identifier: String, sender: Any?)

@objc func longPress(gesture: UILongPressGestureRecognizer) {
        if gesture.state == UIGestureRecognizer.State.began {
            print("Long Press")
            performSegue(withIdentifier: String "showFromLongPress", sender: nil)
        }
    }

这里当然用showFromLongPress你在情节提要中指定的标识符替换标识符。

苹果开发者文档


推荐阅读