简体   繁体   中英

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM