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

标签 ios swift if-statement segue

试图将标签文本从一个 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/

相关文章:

ios - 何时调用 monitoringDidFailForRegion 方法?

objective-c - 通用应用程序 : Should I extract shared Views to their own storyboard

ios - UIImage 显示在模拟器上,而不是在真实设备上

ios - 如何使用swift访问另一个 View Controller 中一个 View Controller 的IBoutlet?

java - 在 IF 语句中定义变量 (Java)

ios - 如何删除 SwiftUI 中列表顶部的空白

ios - Swift 中的 NSURL 数组错误?

swift - 如何取消表格单元格选择

Swift 用户创建未检测 UIText 长度

ios - didSelectRowAtIndexPath 或 prepareForSegue 使用?