Reputation: 5744
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
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
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
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
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