-3

I want to have 3 View Controllers; A, B, and C. View Controller A will be where the final value gets displayed, or the Main View Controller. View Controller B and View Controller C will have simple Text Fields as inputs. The values you put into the Text Fields in View Controller B and C will be added together and shown in View Controller A. You will also need to have buttons to implement the action. How can this be done?

For instance, if the user inputs the number 2 in View Controller B and the number 3 in View Controller C text fields, then View Controller A will show the number 5.

  • 1
    You surely tried *something*. Don't hesitate to show your attempt, so that this does not look like a “write the code for me” question! – ielyamani Oct 06 '18 at 14:08

3 Answers3

0

There are multiple methods to do so

You can use delegate, notification centre etc.

try this reference

Send data back

Send Data Back

and it would be more helpful if you can share your code, what have you done so far.

0

First create the variable "numberOfCViewController" in ViewController B and create two variables called "numberOfCViewControllerInA" and "numberOfBViewControllerInA" in ViewController A

Then just create segues between all the controllers and simply add this function to pass the TextFields data:

override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
    if segue.identifier == "CViewControllerToBViewController"
    {
        let BViewController = segue.destination as! BViewController
        BViewController.numberOfCViewController = numberTextField.text!
    }
}

By doing that you pass the numberTextField.text! over to the ViewController B. All you now have to do is repeat the same techniche in the other ViewControllers until youve reached C.

Then you just simply add the two numbers and assign the result to the Label in ViewController C.

Marie.K
  • 37
  • 8
0

Depends on what your using:

  • Segue:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        guard segue.identifier == "ViewControllerBToViewControllerC" else {
            return
        }
        guard let viewControllerB = segue.destination as? ViewControllerB else {
            return
    }
        viewControllerB.passedValue = <insert passed value>
    }
    
  • Navigation Controller:

    guard let navigationController = navigationController else {
        return
    }
    guard let viewControllerB = UIStoryboard(name: "\(ViewControllerB.self)", bundle: nil) as? ViewControllerB else {
        return
    }
    viewControllerB.passedValue = <insert passed value>
    navigationController.pushViewController(viewControllerB, animated: true)