ios - 使用 If 条件执行 segue 和发送数据

标签 ios swift segue

我有 4 个单元格,每个单元格内都有一个标签。

enter image description here

每次用户单击一个单元格时,都会将他们带到不同的 View ,在该 View 中,我有一个顶部标签,其名称应类似于单元格标签。因此,如果我单击单元格 001,那么下一页标题标签应该是 001。下图显示标题标签:

enter image description here

我使用 prepare for segue 来传递标题标签的值,但是,我该如何编写这样的代码:

if cell label = 001 {
   newViewTitle.text = cell label
}

这是我当前准备的 segue 代码:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if let destination = segue.destination as? VolumeImgVC {


        if let VolumeNumLbl = volumEDnum as? String! {
            destination.Vtitle = VolumeNumLbl
        }
        if let VolumeNumLbl = volumEDnum1 as? String! {
            destination.Vtitle = VolumeNumLbl
        }
        if let VolumeNumLbl = volumEDnum2 as? String! {
            destination.Vtitle = VolumeNumLbl
        }

    }
}

上面的代码不起作用,只执行最后一个if let语句。

有什么建议吗?谢谢

最佳答案

在单击单元格时创建一个 cellnamelabel 数组数组,只需使用 tableView.indexPathForSelectedRow 属性获取选定的 indexPath 并传递数据

   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
       if segue.identifier == "detailSeque" {
          if let destination = segue.destination as? VolumeImgVC {
             if let indexPath = self.tableView.indexPathForSelectedRow {
                let volumeNum = volumeNumLblArray[indexPath.row]
                destination.Vtitle = volumeNum
             }
          }
       }
    }

关于ios - 使用 If 条件执行 segue 和发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45114284/

相关文章:

ios - Sqlite 遍历所有表

iOS 动态创建 UILabel

ios - 如何使应用程序(使用用户名/密码登录)知道用户已登录?

iOS swift popToRootViewController 在 rootViewController 中触发函数

ios - 你在哪里初始化 View Controller 中的属性值?

ios - 如何在 iOS 5 中为 CIAffineTransform 设置 inputTransform

ios - 如何在 UITableViewCell 上显示数组数据?

swift - Swift UINavigationController 子类中的强制初始化覆盖

ios - Swift:如何确保 seque 在 http 任务完成之前不运行?

ios - 从 Swift 中的导航后退按钮解除 segue