UI Button if else statement

Hello,
I am quite new to Swift, and trying to understand, how i can programmatically create an UI Button, that can execute code after the button has been pressed, and stop the code when the button is pressed again (with if else statements).
I have studied the documentation by Apple, but can't quite get how you can achieve this.
Answered by Claude31 in 645697022
There are several ways.

A very simple is to use the tag of the button to keep the state.

tag = 0 : first action
tag = 1 : second action

In the IBAction of the button:
  • test the tag value and execute the appropriate action depending on value 0 or 1

  • change the state by flipping the tag:

Code Block
sender.tag = 1 - sender.tag

Accepted Answer
There are several ways.

A very simple is to use the tag of the button to keep the state.

tag = 0 : first action
tag = 1 : second action

In the IBAction of the button:
  • test the tag value and execute the appropriate action depending on value 0 or 1

  • change the state by flipping the tag:

Code Block
sender.tag = 1 - sender.tag

you can use a process class

UI Button if else statement
 
 
Q