[英]Data from 3 different buttons on a VC to a second VC to be displayed as title
I have have two View Controllers.我有两个视图控制器。 In my VC1 I have 3 buttons that will represent 3 different colors from different colors (Black, Pink, and Orange).
在我的 VC1 中,我有 3 个按钮,它们代表来自不同 colors(黑色、粉色和橙色)的 3 个不同 colors。 The action outcome I want is that, when I click on whichever button, it will take me to my VC2 which would be displaying the name of the button as the title.
我想要的操作结果是,当我单击任何一个按钮时,它会将我带到我的 VC2,它将显示按钮的名称作为标题。
Let's say I tap on the Black button.假设我点击了黑色按钮。 Right after I do it, it would take me from VC1 to VC2 and the title would read Black and it would be the same for the rest of the buttons (Pink, and Orange.)
在我这样做之后,它会将我从 VC1 带到 VC2,标题将显示为黑色,按钮的 rest (粉色和橙色)将是相同的。
This is what I have so far:这是我到目前为止所拥有的:
@IBAction func myBlackButton(_ sender: Any) {
performSegue(withIdentifier: "VCColor", sender: self)
}
@IBAction func myPinkButton(_ sender: Any) {
performSegue(withIdentifier: "VCColor", sender: self)
}
@IBAction func myOrangeButton(_ sender: Any) {
performSegue(withIdentifier: "VCColor", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "VCColor" {
if let destino = segue.destination as? ViewControllerColors {
destino.titulo = "Negro"
}
}
}
} }
-> create a variable on VC2 -> 在 VC2 上创建一个变量
var color: String?
-> Create a function VC1 to send data to another -> 创建一个 function VC1 发送数据到另一个
func buttonTap(Color: String) {
let vc = let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "VC2") as? VC2
vc.color = Color
self.navigationController?.pushViewController(vc!, animated: true)
}
-> call the function in each button -> 在每个按钮中调用 function
@IBAction func myOrangeButton(_ sender: Any) {
buttonTap(Color:"Orange");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.