Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Target action is called twice

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.

like image 774
Lalit_vicky Avatar asked Jan 31 '14 12:01

Lalit_vicky


2 Answers

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.

like image 188
kovpas Avatar answered Oct 21 '22 22:10

kovpas


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];
like image 2
Mani Avatar answered Oct 21 '22 21:10

Mani