简体   繁体   中英

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

Trying to pass label text from one view controller to another, however I only want the text from selected labels to be passed across. The labels are chosen by selecting a check box next to it. My attempt is below. I do not receive any errors but I am not seeing the text in the second VC. It doesn't have to be text off a label I just need to pass text that corresponds with the selected check boxes. I have tried with just setting a var. This is my first attempt at programming so I may be missing something very simple. In short need to be able to perform a prepare for segue within an 'if' statement.

First VC

           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
            }
        }
    }
}

Second 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

Try this:

First VC

       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
    }
}

Second 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}

}

I think below code will help you,Try like this

First VC

if sender.isSelected == true {
      sender.isSelected = false
    } else {
      sender.isSelected = true
      if segue.identifier == "yourIdentifier" {
        var vc = segue.destination as! step4ViewController
        vc.EnergySourceLab = EnSoString
      }
    }

Ok so I have figured out a way to solve this issue. It's a long and convoluted way to reach my desired outcome but it works. On the first View Controller I made a counter then assigned each check box an Int value. Then I passed that Int value to the next View Controller. Then in the next View Controller I assigned each Int value to a String value and put that string in the text box (if counter Int value = X print this text, else print ""). I set up multiple counters on the first View Controller so the user can select multiple check boxes and on the second View Controller referenced each of those counters. If anyone is interested I can post the code. Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM