1
\$\begingroup\$

ViewController2 (source) Has several TextFields, each with their own switches.

ViewController1 (destination) Has one TextView waiting for data to be sent to it from ViewController2 (source) But i need only the text from the TextFields that have their "switches" turned on.

I can get it working individually with TextField1 when Switch1 is turned on, but I don't know how to combine the text from several ones once turned on.

Example: If Switches for both TextField1 and TextField2 are turned on, then it should put the text form both of them into the single TextView on ViewController1

Question How do I combine the text from several ones once turned on ? At time there could be only 1 switch turned on, or at times many switches turned on.

My code that i am using so far

ViewController2 (Source)

 let defaults = UserDefaults.standard
    var switchON : Bool = false
    @IBAction func checkState(_ sender: AnyObject) {

        if Switch1.isOn{
            switchON = true
            defaults.set(switchON, forKey: "switch1ON")
        }
        if Switch1.isOn == false{
            switchON = false
            defaults.set(switchON, forKey: "switch1ON")
        }
        if Switch2.isOn{
            switchON = true
            defaults.set(switchON, forKey: "switch2ON")
        }
        if Switch2.isOn == false{
            switchON = false
            defaults.set(switchON, forKey: "switch2ON")

ViewController1(Destination)

override func viewDidLoad(){
        super.viewDidLoad()
        // Do any additional setup after loading the view.

  // Select Comments -----------------------------------
        if defaults.value(forKey: "switch1ON") != nil{
            let switch1ON: Bool = defaults.value(forKey: "switch1ON")  as! Bool
            if switch1ON == true{
                let userDefaults = UserDefaults.standard
                Reference.text = userDefaults.value(forKey: "PredefinedText1") as? String
            }

        if defaults.value(forKey: "switch2ON") != nil{
            let switch2ON: Bool = defaults.value(forKey: "switch2ON")  as! Bool
            if switch2ON == true{
                let userDefaults = UserDefaults.standard
                Reference.text = userDefaults.value(forKey: "PredefinedText2") as? String
            }
           // else if switch1ON == false{
            //    let userDefaults = UserDefaults.standard
            //    Reference.text = userDefaults.value(forKey: "") as? String
            //}
        }
        }
\$\endgroup\$

Browse other questions tagged or ask your own question.