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?
Than开发者_C百科ks,
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.
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.
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
{
}
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];
}
精彩评论