I have a VC RaceDayChecklistViewController.m which is subclass of RaceDayChecklistViewControllerBase.m.
In RaceDayChecklistVC.m , added a target action which is getting called twice. nextOrNewButton is the button on click of which i want to invoke "demo" action. Also ,checklistnavigationItem is the bar Button item.
- (void)viewDidLoad
{
checklistTableViewBase=checklistTableView;
checklistNavigationItemBase=checklistnavigationItem;
nextOrNewButtonBase=nextOrNewButton;
[nextOrNewButton addTarget:self action:@selector(demo) forControlEvents:UIControlEventAllEvents];
}
-(void) demo
{
RaceDayDataController *sharedController = [RaceDayDataController sharedDataController];
if (sharedController.isSubmited)
{
[self.checklistnavigationItem setTitle:@"New"]; //
}
else
{
[self.checklistnavigationItem setTitle:@"Next"];
[self showAlert];
}
}
-(void) viewWillDisappear:(BOOL)animated
{
[nextOrNewButton removeTarget:self action: @selector(demo) forControlEvents:UIControlEventAllEvents];
}
What could be the reason for multiple call to the action demo? Is it the base class responsible some how?
pls guide.
UIButton
generates multiple events while pressing: usually they are UIControlEventTouchDownInside
and UIControlEventTouchUpInside
. So, if you want to handle press, you should catch the one you need (probably UIControlEventTouchUpInside
), not UIControlEventsAll
.
I can see this
[nextOrNewButton removeTarget:self action: @selector(demo) forControlEvents:UIControlEventAllEvents];
That means, When you touch the button,touch up
event(UIControlEventTouchUpInside
) trigger, so it will fire one time. After that, touch down
will happen, so it will fire again with UIControlEventTouchDownInside
.
So you can use this,
[nextOrNewButton removeTarget:self action: @selector(demo) forControlEvents:UIControlEventTouchUpInside];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With