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 do an If Statement with Actions?

I know how to use it with outlets but how do I do it with actions? Example : How can I make it so that if my button is tapped 3 times, a piece of text will be displayed? This was a bit hard to explain!

try putting the button click event into a while loop and have a variable that gets 1 added to it every revolution. then after the while statement have something like. If x = 3 then do something, else keep going.

1 Answer

Hey Egor,

Something like this would solve your problem. Have an int variable that contains the amount of times a button was tapped and each time the button is tapped the IBAction associated with that button will be executed and you increment your count.

var count: Int = 0 // Amount of times taped
@IBAction func ButtonTaped(sender: AnyObject) {
    count += 1 // Increments the count
    if (count == 3) { // Check amount of taps
        // Display Text Here
    }
}

Hope this helped, if you have any other questions don't hesitate to ask!

Ozzie