ios - 如何在 TableView Cell 中创建长按按钮以进行 segue

标签 ios swift uitableview button storyboard

我只能从 ViewController 创建我的 segue 函数,并且我的 TableViewCell 中必须有 longPress 函数。没有办法在不出错的情况下引用 segue 函数。当按钮保持在 TableView 单元中时,我将如何在 cellForRow 中实现此代码以继续?顺便说一句,我正在使用 Storyboard。

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

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

最佳答案

我找到了一个简单但非正统的解决方案,但就像一个魅力:

将第二个按钮放入您的 TableView 单元中,连接您的 segue,并将其设置为隐藏。然后为它创建您的 IBOutlet,我将其命名为 SegueButton:

@IBOutlet weak var LongPressButton: UIButton!
@IBOutlet weak var SegueButton: UIButton!

现在添加到您的 awakeFromNib:
override func awakeFromNib() {
addLongPressGesture()
}

最后,添加长按按钮代码:
@objc func longPress(gesture: UILongPressGestureRecognizer) {
            if gesture.state == UIGestureRecognizer.State.began {
                print("Segue was Successful")
                SegueButton.sendActions(for: .touchUpInside)
                }
            }

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

关于ios - 如何在 TableView Cell 中创建长按按钮以进行 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61706595/

相关文章:

ios - UITableView 和解析数据

表上的 ios 自定义单元格不显示图像

ios - 为什么 UITableView commitEditing 不起作用?

ios - 在 Swift 中捕获列表

ios - 如何? UISplitViewController TableCell 图像

iphone - 基于 View 的 iOS 应用程序模板

swift - 单元格未出现在 UICollectionView 中

swift - 将 NSMutableSet 存储到 NSUserDefaults

HTTP 后登录失败

ios - "self"在 Swift 中有什么用?