The target action is called twice.

I have a VC RaceDayChecklistViewController.m, which is a subclass of RaceDayChecklistViewControllerBase.m.

A target action has been added to RaceDayChecklistVC.m that is called twice. nextOrNewButton - button, pressing of which I want to trigger the action "demo". In addition, checklistnavigationItem is an element of a panel button.

- (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 a multiple call demonstration of actions? Is this a base class, somehow?

pls manual.

+4
source share
4 answers

UIButton : UIControlEventTouchDownInside UIControlEventTouchUpInside. , , , (, UIControlEventTouchUpInside), UIControlEventsAll.

+5

[nextOrNewButton removeTarget:self action: @selector(demo) forControlEvents:UIControlEventAllEvents];

, , touch up event (UIControlEventTouchUpInside), . touch down , UIControlEventTouchDownInside.

, ,

[nextOrNewButton removeTarget:self action: @selector(demo) forControlEvents:UIControlEventTouchUpInside];
+2

, .

UIControlEventTouchUpInside.

.

        typedef NS_OPTIONS(NSUInteger, UIControlEvents) {
            UIControlEventTouchDown           = 1 <<  0,      // on all touch downs
            UIControlEventTouchDownRepeat     = 1 <<  1,      // on multiple touchdowns (tap count > 1)
            UIControlEventTouchDragInside     = 1 <<  2,
            UIControlEventTouchDragOutside    = 1 <<  3,
            UIControlEventTouchDragEnter      = 1 <<  4,
            UIControlEventTouchDragExit       = 1 <<  5,
            UIControlEventTouchUpInside       = 1 <<  6,
            UIControlEventTouchUpOutside      = 1 <<  7,
            UIControlEventTouchCancel         = 1 <<  8,

            UIControlEventValueChanged        = 1 << 12,     // sliders, etc.

            UIControlEventEditingDidBegin     = 1 << 16,     // UITextField
            UIControlEventEditingChanged      = 1 << 17,
            UIControlEventEditingDidEnd       = 1 << 18,
            UIControlEventEditingDidEndOnExit = 1 << 19,     // 'return key' ending editing

            UIControlEventAllTouchEvents      = 0x00000FFF,  // for touch events
            UIControlEventAllEditingEvents    = 0x000F0000,  // for UITextField
            UIControlEventApplicationReserved = 0x0F000000,  // range available for application use
            UIControlEventSystemReserved      = 0xF0000000,  // range reserved for internal framework use
            UIControlEventAllEvents           = 0xFFFFFFFF
        };
0

As you set UIControlEventAllEvents, therefore, when you touch your button, two types of touch are required, so you should use only one type, Eventeither UIControlEventTouchUpInside, or UIControlEventTouchUpOutside.

[nextOrNewButton addTarget:self action:@selector(demo) forControlEvents: UIControlEventTouchUpInside];
0
source

Source: https://habr.com/ru/post/1524696/


All Articles