What is the convention to handle secondary action of UIButton in iOS

166 views Asked by At

I have a UIButton which is used to do something when user touch the button. I did it in Touch Up Inside. I have to implement another function which is treated as like a secondary action for that UIButton.

What is the convention in iOS to handle more than one action for same UIButton? Should I use

double-tap action

or

long press gesture

or anything else?

3

There are 3 answers

0
Ashish Sahu On

Set tag of the button inside method, set default tag is 0,

- (IBAction)action:(id)sender {
    UIButton *mybtn = (UIButton*)sender;
    if (mybtn.tag == 0) {
        //perform fist action
        //set btn tag to 1
        mybtn.tag = 1;
    }
    else if (mybtn.tag == 1)
    {
        //perform second action
        //set btn tag to 0
        mybtn.tag = 0;
    }
5
Zaid Pathan On

As Imad suggested, use a bool called isClickedOnce (default is false), when user click first time then set value to true.

var isClickedOnce = false

func switchAction(){
    if isClickedOnce{
      print("Second")
    }else{
      isClickedOnce = true
      print("First")
    }
}
0
Vincent On

For maintaining the App I would not advise implementing 2 IBactions on a button. I would suggest creating an IBAction routine which initiates both actions.