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\$

closed as off-topic by Roland Illig, dfhwze, Martin R, Toby Speight, pacmaninbw Aug 19 at 16:17

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Roland Illig, dfhwze, Martin R, Toby Speight, pacmaninbw
If this question can be reworded to fit the rules in the help center, please edit the question.

Browse other questions tagged or ask your own question.