0

I know, on YouTube a lot of videos about how to Passing Data Between View Controllers like that. But that is not what I need. I'm new on this website and in Xcode. I didn't find answer on my question here. My App move from VCA to VCB with a segue. I need pass Calculated Value from ViewControllerA.swift to ViewControllerB.swift Example of my code in ViewControllerA.swift

let weight = Double(textField.text!)
let calculatedValueA = weight! * 2
let calculatedValueB = weight! * 3
let calculatedValueC = weight! * 4

In ViewControllerB.swift in override func viewDidLoad() will see results like that I guess

calValueA.text = String(format: "%4.0f", calculatedValueA)
calValueB.text = String(format: "%4.0f", calculatedValueB)
calValueC.text = String(format: "%4.0f", calculatedValueC)
  • How is your app constructed? What is the relationship of VCA and VCB? Are they elements of a tab bar controller? Does the app move from VCA to VCB with a segue? – vacawama May 18 '18 at 12:59
  • Hello! This is the same app [link](https://stackoverflow.com/questions/50328507/different-viewcontroller-if-different-selectedsegmentindex/) – Laura Manukyan May 18 '18 at 13:03
  • Do you need I show screens or parts of code? – Laura Manukyan May 18 '18 at 13:04
  • Just update your question to describe how you move from VCA to VCB. Saying, I'm moving with a segue would be enough. – vacawama May 18 '18 at 13:06
  • @vacawama done. – Laura Manukyan May 18 '18 at 13:09
  • See this answer: https://stackoverflow.com/a/31934786/1630618 – vacawama May 18 '18 at 13:16
  • It's not what I want. I said it in description. Even put link to video on YouTube – Laura Manukyan May 18 '18 at 13:43
  • send only textField.text is definitely not what I need because my calculated value depends of UISegmentedControl and user check it on the VCA, not VCB – Laura Manukyan May 18 '18 at 13:46
  • In `prepare(for:sender:)` assign `calculatedValueA`, `calcualtedValueB` and `calculatedValueC` to similarly named properties in VCB. – vacawama May 18 '18 at 14:01
  • there just info from textField.text. In my VCA all calculated values inside of UIButton **@IBAction func acceptButton(_ sender: Any) {** Or I am too silly for that :) – Laura Manukyan May 18 '18 at 15:28
  • It's fine to calculate the values in the UIButton @IBAction, but store them in properties of the VCA instead of variables local to the button code, and then you'll be able to access them and pass them on in `prepare(for:sender:)`. – vacawama May 18 '18 at 16:19
  • I got just empty in VCB – Laura Manukyan May 19 '18 at 03:03
  • When I put **func prepare(for segue: UIStoryboardSegue, sender: Any?) {** and **secondViewController.calculatedValue = calculatedValue** in to my button (after **if mySegment.selectedSegmentIndex == 0 {**) then I got **Cannot assign value of type 'Double' to type 'String'** even I have in VCB **var calculatedValue = ""** and **label.text = String(format: "%4.0f", calculatedValue)** – Laura Manukyan May 19 '18 at 03:20

2 Answers2

0

One way is that you need to create properties in ViewControllerB.swift

var valueA: Double?
var valueB: Double?
var valueC: Double?

When you are navigating form VCA to VCB then you need to set these properties with respected values.

let vcb = VCA viewcontroller....
vcb.valueA = weight! * 2
vcb.valueB = weight! * 3
vcb.valueC = weight! * 4
self.navigationController.pushViewController...

Note: It is not good practise to unwrap optional value forcefully, you can use if let or guard to save app from crashes.

You can use delegation or closure to pass data to back to calling object.

Mahendra
  • 8,448
  • 3
  • 33
  • 56
0

As I understand your question, you can do this:

    class ViewControllerA: UIViewController {
    @IBOutlet weak var textField: UITextField!

    func sendValuesToB(){
        let viewControllerB = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerB") as! ViewControllerB
        //calculate values
        let weight = Double(textField.text!)
        let calculatedValueA = weight! * 2
        let calculatedValueB = weight! * 3
        let calculatedValueC = weight! * 4

        //set the value of view controller B
        viewControllerB.calculatedValueA = calculatedValueA
        viewControllerB.calculatedValueA = calculatedValueB
        viewControllerB.calculatedValueA = calculatedValueC
        self.navigationController?.pushViewController(viewControllerB, animated: true)
    }
}

class ViewControllerB: UIViewController {
    @IBOutlet weak var calValueA: UILabel!
    @IBOutlet weak var calValueB: UILabel!
    @IBOutlet weak var calValueC: UILabel!

    var calculatedValueA : Double!
    var calculatedValueB : Double!
    var calculatedValueC : Double!

    override func viewDidLoad() {
        super.viewDidLoad()
        calValueA.text = String(format: "%4.0f", calculatedValueA)
        calValueB.text = String(format: "%4.0f", calculatedValueB)
        calValueC.text = String(format: "%4.0f", calculatedValueC)
    }
}
Nakul Sharma
  • 600
  • 1
  • 16
  • 23