gpt4 book ai didi

ios - Pass label text from one view controller to another using segues only if button (checkbox) is selected

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:03:34 25 4
gpt4 key购买 nike

试图将标签文本从一个 View Controller 传递到另一个 View Controller ,但我只希望传递选定标签中的文本。通过选中旁边的复选框来选择标签。我的尝试如下。我没有收到任何错误,但我没有在第二个 VC 中看到文本。它不必是标签上的文本我只需要传递与所选复选框对应的文本。我试过只设置一个变量。这是我第一次尝试编程,所以我可能会遗漏一些非常简单的东西。简而言之,需要能够在“if”语句中执行 segue 准备。

第一位风投

           let EnSoString = "Energy Source"
@IBAction func EnergySourceBut(_ sender: UIButton) {

if sender.isSelected {
sender.isSelected = false
} else {
sender.isSelected = true;do {
func prepare(for segue: UIStoryboardSegue, sender: Any){
let receivevc1 = segue.destination as! step4ViewController
receivevc1.EnergySourceLab = EnSoString
}
}
}
}

第二个VC

    //name
var EnergySourceLab:String?

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
// received info
if let receivedEnergySourceLab = EnergySourceLab {
EnergySourceLab1.text = receivedEnergySourceLab}

}

Selecting checkbox Code for prepare for segue within button is.selected

最佳答案

试试这个:

第一位风投

       let EnSoString = "Energy Source"

@IBAction func EnergySourceBut(_ sender: UIButton) {

if sender.isSelected {
sender.isSelected = false
} else {
self.performSegue(withIdentifier: "segueID", sender: self)
}
}



override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueID" {
if let vc = segue.destinationViewController as? YourSecondVC {
vc.EnergySourceLab = EnSoString
}
}

第二个VC

//name
var EnergySourceLab:String?

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
// received info
if let receivedEnergySourceLab = EnergySourceLab {
EnergySourceLab1.text = receivedEnergySourceLab}

}

关于ios - Pass label text from one view controller to another using segues only if button (checkbox) is selected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49789049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com