-5

I have one ViewController.swift file and two View Controller on Main.Storyboard. Of course there is a Navigation Controller. Both View Controller have a Class: ViewController.

It all looks something like this

I enter the value in the TextField, press Enter, there is mathematical calculation and I see the result on the Label. However, I can see the result in the Label only if the TextField and Label are on the same View Controller.

How do I make it to see the result in the Label on the second View Controller after clicking Next?

Kaushik Makwana
  • 1,329
  • 2
  • 14
  • 24

2 Answers2

0

You need to pass those values to the second ViewController.

To do this, in your first ViewController, you need to override the method override func prepare(for segue: UIStoryboardSegue, sender: Any?) and identify of your about to segue to the second ViewController and set the values.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let viewController = segue.destinationViewController as? SecondViewController {
        viewController.calculatedValue = self.calculatedValue
    }
}

When the second ViewController loads, it will have these already set.

barnabus
  • 862
  • 7
  • 26
  • Thank you. In new Swift only "destination" instead "destinationViewController". But anyway it's didn't work. In line **viewController. calculatedValue = calculatedValue** says **Value of type 'ViewControllerB' has no member 'calculatedValue'** even I have **var calculatedValue = String()** in my ViewControllerB – Dave Ivanov May 13 '18 at 13:01
  • Can you paste your code – barnabus May 13 '18 at 21:49
  • This is ViewControllerA `@IBAction func pressButton(_ sender: Any) { self.resignFirstResponder() if segmentSwitch.selectedSegmentIndex == 0 { let temperature = Double(textField.text!) let lowTempNum = temperature! * 10 let highTempNum = temperature! * 30 lowTemp.text = String(format: "%4.0f", lowTempNum) highTemp.text = String(format: "%4.0f", highTempNum)` – Dave Ivanov May 14 '18 at 02:30
  • of course I use `var lowTempNum = String()` and `var highTempNum = String()` in **ViewControllerB** – Dave Ivanov May 14 '18 at 09:41
0

Create a var in ViewController B and assign result of ViewController A with variable of viewcontroller B. Like In ViewController B: var result = "" in ViewDidLoad()

label.text = result

In ViewController A When enter button pressed

let vc = ViewControllerB(nibName: "ViewControllerB", bundle: nil)
vc.result = //your calculated result
self.navigationController?.pushViewController(vc, animated: true)
Taimoor Suleman
  • 1,588
  • 14
  • 29
  • Thank you. But still cannot win it. My app crashed right after I click my button. In line "class AppDelegate: UIResponder, UIApplicationDelegate {" --- *Thread 1: signal SIGABRT* – Dave Ivanov May 13 '18 at 12:36