Passing Int Value from SecondVC to FirstVC

How can I pass a modified Int value from the secondVC to the firstVC?

I can pass the variable in its original form, however I’ve created a clicker to increase or decrease the variable numerically. Whenever I call the variable it always calls the original variable value of 2 and not the newly selected integer from the clicker in the SecondViewController. Why is this?

The variable I’m trying to pass is:

var targetsUpper

FirstVc

// MARK: Variables
let secondVC = SecondViewController()


// MARK: IB Outlets
@IBOutlet weak var startButton: UIButton!
@IBOutlet weak var mainLabel: UILabel!

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

//MARK: Start Round
@IBAction func startRoundTapped(_ sender: Any) {
    
    
    // Randomize the range of the Targets
    let targetRange = Int.random(in: 1...secondVC.targetsUpper)
    
    // Set the mainLabel to display the called Target
    mainLabel.text = String(targetRange)
}

SecondVC

//MARK: Variables
var targetsUpper = 2
var calls = 1



//MARK: IB Outlets

// Targets Label
@IBOutlet weak var targetLabel: UILabel!

// Add & Subtract Target Buttons
@IBOutlet weak var addTargetButton: UIButton!
@IBOutlet weak var subtractTargetButton: UIButton!


// MARK: viewDidLoad
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    
    // Load the view with the subtractTargetButton Disabled
    subtractTargetButton.isEnabled = false
    
    // Set the targetLabel to the targets value
    targetLabel.text = String(targetsUpper)
    
}


//MARK: Add or Subtract Targets

// Subtract Targets
@IBAction func subtractTargetTapped(_ sender: Any) {
    
    
    // Create the lower Limit (2)
    if targetsUpper == 2 {
        targetsUpper += 0
        targetLabel.text = String(targetsUpper)
    }
    else {
        targetsUpper -= 1
        targetLabel.text = String(targetsUpper)
    }
    
    
    // Disable the subtractTargetButton at Lower Limit (2)
    if targetsUpper == 2 {
        subtractTargetButton.isEnabled = false
    }
    
    // Enable the addTargetButton when not at Upper Limit (10)
    if targetsUpper < 10 {
        addTargetButton.isEnabled = true
    }
    
    // Debugging purposes
    print(targetsUpper)
   
}

// Add Targets
@IBAction func addTargetTapped(_ sender: Any) {
    
    // Create the Upper Limit (10)
    if targetsUpper == 10 {
        targetsUpper += 0
        targetLabel.text = String(targetsUpper)
    }
    else {
        targetsUpper += 1
        targetLabel.text = String(targetsUpper)
        
    }
    
    
    // Enable subtractButton when not at Lower Limit (2)
    if targetsUpper > 2 {
        subtractTargetButton.isEnabled = true
    }
    
    // Disable the addTargetButton at Upper Limit (10)
    if targetsUpper == 10 {
        addTargetButton.isEnabled = false
    }
    
    // Debugging purposes
    print(targetsUpper)

}

Okay so your second view controller working fine, but then your first view controller isn’t presenting targetsUpper that was modified in the secondVC?

You need to pass the variable back to the first view controller

You could use a protocol/delegate or other methods, this video may help

Thank you for the help :slight_smile:

1 Like