iosxcodeswiftswift2xcode7-beta6

Is it possible to make a button execute a particular action only when pressed for the first time?


I connected a function, which contains an if and else statement, to a button; but I also want to add another action that the button executes only when it is pressed for the first time, i.e. I only want it to perform the if-else when it's tapped for a second, third etc. time.


Solution

  • First, extract your logic into separate function - it will be easier for you to manage it.

    Second, add boolean to the controller called firstTimePressed :

    var firstTimePressed : Bool = false
    

    in your button action add the following:

    if(firstTimePressed == false){
        firstTimePressed = true
    }
    else{
        //call your function
    }
    

    And that's it.