我在我的自定义 tableViewCell 上添加了一个按钮,我希望在点击该按钮后它根据按钮所在的单元格传递数据,以便找出按钮所在的单元格我添加了一个标签,但是我收到这个错误
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToLast" && sender is IndexPath {
let dvc = segue.destination as! FinalClass
dvc.index = (sender as! IndexPath).row
dvc.places = sortedArray
dvc.userLocation = currentLocation
}
if segue.identifier == "goToReview" {
let index3 = IndexPath(row: sender.tag, section: 0)
let rev = segue.destination as! ReviewClass
}
}
如何调整?
(cell.reviewButton.tag = indexPath.row
) 这是我在 cellForRowAt 中添加的标签
更新
这是我VC中的按钮 Action
@IBAction func ReviewButtonTap(_ sender: UIButton) {
performSegue(withIdentifier: "goToReview" , sender: self)
}
这是我的自定义 tableViewCell 类中的 socket
@IBOutlet weak var reviewButton: UIButton!
我不明白如何将此按钮与
关联else if segue.identifier == "goToReview", let button = sender as? UIButton, let rev = segue.destination as? ReviewClass {
let index3 = IndexPath(row: button.tag, section: 0)
最佳答案
强制将可选的 Any
向下转换为预期的 UIButton
和 IndexPath
并使用 else
子句而不是 如果
两次
if segue.identifier == "goToLast" {
let dvc = segue.destination as! FinalClass
let indexPath = sender as! IndexPath
dvc.index = indexPath.row
dvc.places = sortedArray
dvc.userLocation = currentLocation
}
else if segue.identifier == "goToReview" {
let button = sender as! UIButton
let index3 = IndexPath(row: button.tag, section: 0)
let rev = segue.destination as! ReviewClass
}
或使用开关
switch segue.identifier {
case "goToLast":
let dvc = segue.destination as! FinalClass
let indexPath = sender as! IndexPath
dvc.index = indexPath.row
dvc.places = sortedArray
dvc.userLocation = currentLocation
case "goToReview":
let button = sender as! UIButton
let index3 = IndexPath(row: button.tag, section: 0)
let rev = segue.destination as! ReviewClass
default: break
}
代码不得崩溃。如果是的话,你犯了一个设计错误。
编辑
您必须将 UIButton
实例(sender
)从 IBAction
传递到 sender
参数执行转义
。 socket 无关紧要。
performSegue(withIdentifier: "goToReview" , sender: sender)
关于ios - 类型 'Any?' 的值没有成员 'tag',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48206497/