ios - 类型 'Any?' 的值没有成员 'tag'

标签 ios swift uitableview swift4

我在我的自定义 tableViewCell 上添加了一个按钮,我希望在点击该按钮后它根据按钮所在的单元格传递数据,以便找出按钮所在的单元格我添加了一个标签,但是我收到这个错误

enter image description here

 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 向下转换为预期的 UIButtonIndexPath 并使用 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/

相关文章:

ios - 我设置 ScrollView 偏移量以显示被键盘隐藏的文本字段。如果用户在显示键盘时滚动,则 ScrollView 会弹回

iphone - 一次仅允许有一个 UITableViewCell 附件复选标记

当 uitableviewcell 中 uipickerview 的值发生变化时,我需要在 viewcontroller 中更新标签

ios - iOS 13 "canOpenURL: failed for URL: “fbauth2:///” 的 Facebook 登录套件“

iphone - UIRefreshControl 问题

swift - 使用选择器时引用不明确

ios - 找不到 dyld 符号 DictionaryGenerator (IOS Xcode6 Beta4)

ios - 如何调用嵌入了UICollectionView的UITableViewCell的indexPath.row

ios - 本地存储数据 IOS

ios - Storyboard 中 UIViewController 上的异常栏。它是什么?