I'm trying to create a calculator that displays a result to the user about their college grades. The user taps buttons and based on their selections, the UI view updates and displays the next question. My code works when up until I need it to access a previously tapped button that was selected previously and on a different view controller. The problem that I'm having specifically is on one view controller, the user can select if they want to know Calculation1 (associated with top button with sender.tag == 1) or Calculation2 (bottom button, sender.tag == 2). Regardless of which button is pressed, a segue is performed to the same question on a new view controller. After that mutual question is answered, there is only one button to press. I'm trying to code an if statement for if Calculation1 (from previous VC) was selected then update the view to the next question, and if Calculation2 was selected perform a segue to a different VC for the next question. The way I was trying to use the boolean was as a property from the previous VC. The if statement isn't working and instead, the same action happens regardless of the selection.
This is for an iOS app with Swift using Xcode. I attempted to create a boolean as a property so that when Calculation1 is tapped, findCalc1 = true and then accessing that boolean as a property in the next VC but it didn't work. I also tried preparing a boolean for the segue to the mutual question but it didn't work either. I feel like I have a gap in my understanding of what can and cannot be performed between different VCs.
// Here is my code for the first VC
@IBAction func lightButtonPressed(_ sender: UIButton) {
if sender.tag == 1 && lightViewPromptIndex == 1 {
desiredGpaText = textfield.text!
performSegue(withIdentifier: "goToDarkButtonVC", sender: self)
} else if sender.tag == 2 && lightViewPromptIndex == 1 {
performSegue(withIdentifier: "goToDarkButtonVC", sender: self)
}
// Here is my code for the second VC
@IBAction func darkButtonPressed(_ sender: Any) {
if aboveTopPromptIndex == 1 && lightViewObject().findCalc1 == true {
aboveTopTextPrompt.text = aboveTopPrompt2
topTextfield.placeholder = "Ex: 76.00"
besideTopTextLabel.isHidden = true
underTopTextLabel.text = "course credits"
aboveBottomTextPrompt.text = "that count toward my GPA."
bottomTextfield.isHidden = true
underBottomTextLabel.isHidden = true
bottomFloatingLabel.isHidden = true
darkButton.setTitle(nextTitle, for: .normal)
aboveTopPromptIndex = 2
} else if aboveTopPromptIndex == 1 && lightViewObject().findCalc1 == false {
performSegue(withIdentifier: "darkViewToABC", sender: TextfieldTwoLightButtonsViewController.self)
The mutual question has aboveTopPromptIndex == 1. What I would like to happen is if that question is asked and Calculation1 was previously selected, for the first chunk of code in the if statement to run... and if Calculation2 is desired, for the segue in the else if statement to occur. Instead, no matter which button is pressed, the first part of the if statement happens, regardless of if Calculation2 was selected previously.
Aucun commentaire:
Enregistrer un commentaire