-1

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.

Glycerius
  • 185
  • 2
  • 7
  • Do you mean first time on every launch or first time per installation? (Or maybe first time after each display of the view controller?) – Phillip Mills Sep 04 '15 at 13:26
  • I meant first time on every launch, but NickCatib already answered my question. Thanks anyway! – Glycerius Sep 04 '15 at 13:45

2 Answers2

2

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.

Miknash
  • 7,888
  • 3
  • 34
  • 46
  • Got it, I understood how silly was the question actually the second I saw your answer. I probably should've thought in a much simpler way. Thanks a lot! – Glycerius Sep 04 '15 at 13:33
  • glad to help, don't forget to close this question :) – Miknash Sep 04 '15 at 13:37
  • I can't though, my rep is not high enough. Thank you for your solution once again. – Glycerius Sep 04 '15 at 13:43
  • well, you need to wait certain amount of time to be able to mark question as accepted ( that checkmark you see). Happy coding – Miknash Sep 04 '15 at 13:46
-3

Build a subclass a UIButton with a BOOL that will allow to perform the action if it's true (or false, whatever you prefer), and once you perform the action for first time change the value of it, so next time won't run the action.

Pablo A.
  • 2,042
  • 1
  • 17
  • 27