目标操作被调用两次

时间:2014-01-31 12:24:38

标签: ios iphone objective-c

我有一个VC RaceDayChecklistViewController.m,它是RaceDayChecklistViewControllerBase.m的子类。

在RaceDayChecklistVC.m中,添加了一个被调用两次的目标动作。 nextOrNewButton是点击的按钮,我想调用“demo”动作。 另外,checklistnavigationItem是栏按钮项。

- (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];
}

多次调用动作演示可能是什么原因?是基类负责的吗?

请指导。

4 个答案:

答案 0 :(得分:6)

UIButton会在按下时生成多个事件:通常是UIControlEventTouchDownInsideUIControlEventTouchUpInside。所以,如果你想处理新闻,你应该抓住你需要的那个(可能UIControlEventTouchUpInside),而不是UIControlEventsAll

答案 1 :(得分:2)

我可以看到这个

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

这意味着,当您触摸按钮时,touch up事件(UIControlEventTouchUpInside)会触发,因此会触发一次。之后,touch down将会发生,因此会再次使用UIControlEventTouchDownInside启动。

所以你可以使用它,

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

答案 2 :(得分:0)

您正在聆听所有活动,因此您将收到所有活动。

通常要捕捉按钮点击事件,我们会有 UIControlEventTouchUpInside

Here is the option for you.

        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
        };

答案 3 :(得分:0)

当您设置UIControlEventAllEvents这就是为什么当您触摸按钮时,它需要两种类型的触摸,因此您只应使用Event UIControlEventTouchUpInsideUIControlEventTouchUpOutside中的一种[nextOrNewButton addTarget:self action:@selector(demo) forControlEvents: UIControlEventTouchUpInside];

{{1}}