Ashutosh
Ashutosh

Reputation: 5744

The same button should do multiple task

I want one of my button to act different on different taps. Because its the same button i am using every time a particular action happens.

Is there a way to do this?

Thanks,

Upvotes: 1

Views: 475

Answers (4)

Waqas Raja
Waqas Raja

Reputation: 10872

You can use the tag property of button; so inside your IBAction method.

-(void)buttonClicked:(id)sender{
    UIButton *button = (UIButton *)sender;
    if (button.tag == 1) {
        // perform your required functionality
        button.tag = 2;
    }
    else if (button.tag == 2) {
        // perform your required functionality
        button.tag = 3;
    }
    else if (button.tag == 3) {
        // perform your required functionality
        button.tag = 1;
    }
}

And don't forget to set initial tag value to 1.

Upvotes: 2

Alex Wayne
Alex Wayne

Reputation: 186984

The button just calls a method in your view controller when tapped. From there you do something like this:

if (internalState == FOO) {
  [self doA];
} else {
  [self doB];
}

Upvotes: 0

nacho4d
nacho4d

Reputation: 45078

Add an additional UIGestureRecognizer ;) Single tap is the action what will be linked but you can add other kind of gestures like double tap, swipe, etc.

Upvotes: 2

JFoulkes
JFoulkes

Reputation: 2429

If you want your button to act different you would create different methods to do the different actions. Then whenever you want the buttons behaviour to change you should set the button to handle the desired action.

So for the first action:

[button addTarget:self action:@selector(method1:) forControlEvents:UIControlEventTouchUpInside];

- (void) method1
{
//set button to handle method 2
[button addTarget:self action:@selector(method2:) forControlEvents:UIControlEventTouchUpInside];
}

- (void) method 2
{

}

Upvotes: 1

Related Questions