在Swift中通过segue传递多个变量

时间:2017-06-27 21:28:29

标签: ios swift segue viewcontroller

我见过很多教程,人们通过segue将单个变量传递给单独的视图控制器。但是,如何将整个结构或类别的信息发送到单独的segue?并在另一边打开它?示例:在表格视图中,我单击一个玩家的名字,它将在另一个视图控制器中显示细节?

示例:棒球名单阵容......

class Player {
    var playerName: String = ""
    var playerPosition: String = ""
    var playerImage: UIImage = ""
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let selectedPlayer = player[indexPath.row].playerName

    performSegue(withIdentifier: "player", sender: selectedPlayer)
}

1 个答案:

答案 0 :(得分:1)

您需要使用override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "player" { if let player = sender as? Player { let secondViewController = segue.destination as! SecondViewController secondViewController.player = player } } } 功能:

performSegue

当您致电secondViewController时,系统会调用此功能。在prepare中声明播放器变量,您只需在didSelectRowAt函数中传递变量。

let selectedPlayer = player[indexPath.row].playerName 更改中:

let selectedPlayer = player[indexPath.row]

要:

Player

因此,您传递的是整个playerName对象,而不仅仅是awk -F'[[:space:]][[:space:]]+' 'NR==FNR{ a[$1]=$2; next } $1 in a{ $2=" "a[$1] }1' Test.csv Test.html