Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How can I hide a button when another button is pressed on a navigation bar?

The title says it all Thanks

2 Answers

Daniel Sattler
Daniel Sattler
4,867 Points

No problem.

You would have to add an IBOutlet AND an IBAction for all affected buttons.

assuming you have 3 buttons, Button One, Button Two and a button to reset everything, so to show all buttons again.

This is how your code would look like:

    @IBOutlet weak var buttonOne: UIButton!
    @IBOutlet weak var buttonTwo: UIButton!

    @IBAction func buttonOnePressed() {

        // When buttonOne is pressed, it hides buttonTwo.

        buttonTwo.hidden = true
    }



    @IBAction func buttonTwoPressed() {

        // When buttonTwo is pressed, it hides buttonOne.

        buttonOne.hidden = true
    }



    @IBAction func unhideButtons() {

        // when unhideButtons is pressed, it unhides all buttons

        buttonOne.hidden = false
        buttonTwo.hidden = false
    }

I hope this leads you in the right direction ;-)

Hehehe yeah haha thanks!

Daniel Sattler
Daniel Sattler
4,867 Points

i give you a hint:

buttonOne.hidden = true   // hides buttonOne
buttonTwo.hidden = false  // shows buttonTwo

I hope this leads you in the right direction ;-)

Haha im really new to programming, can u please show me the whole block of code haha, cheers