swiftshowhiddenviewdidloaduielement

Is there a way to show a UIelement that was previously hidden in earlier lines of code after a button has been tapped on the same VC in Swift?


I have many UI elements on a VC including labels, textfields, and a button. In the viewDidLoad() function, most of the UI elements are initially hidden. I have successfully programmed the UIButton on the VC to change the label text and button title properties depending on what question is being asked. However, when I try to set a text property for a label or textfield that was previously hidden, it does not show back up. I saw on the apple dev website something about a hiddenOrHasHiddenAncestor view but am not sure if that's the solution. Is there an easy fix to this?

I tried creating a Boolean variable as var firstQuestionAsked = false and then setting it to true in the chunk of code when I want the UI elements to show again and setting the viewDidLoad code to an if-statement so that if the Bool is false, the elements are hidden but that didn't work.

// This is my code in the viewDidLoad function

override func viewDidLoad() {
    super.viewDidLoad()

        aboveTopTextPrompt.text = aboveTopPrompt1
        topTextfield.placeholder = "Ex: 2.98"
        besideTopTextLabel.isHidden = true
        underTopTextLabel.isHidden = true
        aboveBottomTextPrompt.isHidden = true
        bottomTextfield.isHidden = true
        underBottomTextLabel.isHidden = true
        bottomFloatingLabel.isHidden = true
        darkButton.setTitle(nextTitle, for: .normal)
    }
}

// This is the portion of my code that is not working (in the button IBAction)

@IBAction func darkButtonPressed(_ sender: UIButton) {

    if aboveTopPromptIndex == 1 {
        aboveTopTextPrompt.text = aboveTopPrompt2
        topTextfield.placeholder = "Ex: 76.00"
        besideTopTextLabel.isHidden = true
        underTopTextLabel.text = "string"
        aboveBottomTextPrompt.text = "string"
        bottomTextfield.isHidden = true
        underBottomTextLabel.isHidden = true
        bottomFloatingLabel.isHidden = true
        darkButton.setTitle(nextTitle, for: .normal)
        aboveTopPromptIndex = 2
    } else if aboveTopPromptIndex == 2 {
        performSegue(withIdentifier: "darkViewToABC", sender: self)
    } else if (aboveTopPromptIndex == 5 || aboveTopPromptIndex == 6 {
        aboveTopPromptIndex = 7
        aboveTopTextPrompt.text = aboveTopPrompt7
        topTextfield.placeholder = "string"
        besideTopTextLabel.text = "string"
        underTopTextLabel.text = "string"
        aboveBottomTextPrompt.text = "string"
        bottomTextfield.placeholder = "string"
        underBottomTextLabel.text = "string"
        bottomFloatingLabel.text = "string"
        darkButton.setTitle(calculateTitle, for: .normal)
    }

The strings I set for the UI elements remain hidden. I did not show all of my code to avoid redundancy but basically, any placeholder or text property that was hidden previously does not become visible when I want it to and I do need them to show for one of the questions as shown in the last else if statement.


Solution

  • someUIElement.isHidden = false will make it visible. The isHidden value determines whether the view is hidden or not. It won't be changed automatically by any means. So once you set it to true, it won't be visible again until you explicitly set it back to false