我只能从 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/