2

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

MRizwan33
  • 2,723
  • 6
  • 31
  • 42
Tom Kat
  • 21
  • 1
  • You are passing string as soon the CheckBox is Clicked , You want to pass only one text Or multiple ? – iOS Geek Apr 12 '18 at 06:12
  • Refer selected answer https://stackoverflow.com/questions/44096117/swift-prepare-for-segue-not-passing-data/44100011#44100011 – Alwin Apr 12 '18 at 06:16
  • Possible duplicate of [Pass data through segue](https://stackoverflow.com/questions/26207846/pass-data-through-segue) – Lal Krishna Apr 12 '18 at 08:33
  • I do not wish to perform the segue as soon as the checkbox is selected. The user needs to be able to select multiple boxes and the text from each be passed to the next view controller. For example if I had 10 check boxes each labelled "One", "Two", "Three"... and the user selected one and two, this would show on the next view controller "One" and "Two". – Tom Kat Apr 16 '18 at 04:10

3 Answers3

0

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}

}
MRizwan33
  • 2,723
  • 6
  • 31
  • 42
  • Thank you for your response. If I do the code above wont I be performing the segue as soon as the check box is selected? I need to be able to check multiple boxes and then perform the segue.. with the text from each of the buttons (checkboxes) – Tom Kat Apr 16 '18 at 04:13
0

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
      }
    }
Naren Krish
  • 259
  • 2
  • 14
0

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

Tom Kat
  • 21
  • 1