我见过很多教程,人们通过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)
}
答案 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
。