简体   繁体   English

从 VC 上的 3 个不同按钮到要显示为标题的第二个 VC 的数据

[英]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.

相关问题 在VC1中从Firebase检索用户数据并将其发送到VC2中的TableView时出现问题 - Problem retrieving users data from firebase in VC1 and sending them to TableView in VC2 to be displayed 将相同的数据从一个 VC 传递到另一个 VC 到另一个 VC - Pass same data from one VC to another VC to another VC 将数据从第二个VC传递到第一个VC,并在文本字段Swift4中显示 - Pass data from second VC to first VC and also show in textfield Swift4 第二个 vc 比初始 vc 小 - second vc is smaller that the initial vc 在1个VC中按下了UIButton,我需要它来将按钮标题保存到标签中的另一个视图控制器中,而不是直接彼此隔离 - A UIButton is pressed in 1 VC and I need it to save the buttons title to a label in a different view controller not directly segued to each other 从第二个VC解散后,第一个VC中的工具栏未出现 - Toolbar in first VC not appearing after unwind segue from second VC 将数据传递给不是之前 VC 的 VC - Pass data to VC that is not the previous VC 将数据从没有Segue的VC传递到非VC类 - Pass data from a VC without Segue to a non VC Class 在XLPagerTabStrip中将数据从Container VC传递到子VC - Pass Data from Container VC to Child VC in XLPagerTabStrip Swift - 使用 Firestore 将数据从 VC 传递到详细信息 VC - Swift - passing data from VC to detail VC with Firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM