Swift: How can I declare a statement based off the title of a button?

112 Views Asked by At

I have a button that's title is set to "calculator" When this button in my view controller is tapped, it hides and shows a bunch of objects and it changes to the title of the button to "back":

@IBAction func calculatorButtonTapped(sender: AnyObject) {
    calculatorContainer.hidden = false
    inspirationLabel.hidden = true
    beginningLabel.hidden = true
    menuExampleButton.hidden = true
    aboutButton.hidden = true
    calculatorButton.setTitle("Back", forState: UIControlState.Normal)
}

When the title is set to "back", and when that button is clicked, I want everything that was hidden to be shown and everything that was shown to be hidden. When the title is set to "calculator", I want everything from the above code to occur. How can I do this?

1

There are 1 best solutions below

0
On

You can try this code:

@IBAction func calculatorButtonTapped(sender: AnyObject) {
    calculatorContainer.hidden = !calculatorContainer.hidden
    inspirationLabel.hidden = !inspirationLabel.hidden
    beginningLabel.hidden = !beginningLabel.hidden
    menuExampleButton.hidden = !menuExampleButton.hidden
    aboutButton.hidden = !aboutButton.hidden
    calculatorButton.setTitle("Back", forState: UIControlState.Normal)


    if !calculatorContainer.hidden{

            inspirationLabel.hidden = true
            beginningLabel.hidden = true
            menuExampleButton.hidden = true
            aboutButton.hidden = true

    } 
}

This will switch back and forth between hidden and not hiding your image.