Ios 目标操作被调用两次
我有一个VC RaceDayChecklistViewController.m,它是RaceDayChecklistViewControllerBase.m的子类 在RaceDayChecklistVC.m中,添加了一个目标操作,该操作将被调用两次。nextOrNewButton是我想要调用“demo”操作的单击按钮。 此外,checklistnavigationItem是条形按钮项Ios 目标操作被调用两次,ios,iphone,objective-c,Ios,Iphone,Objective C,我有一个VC RaceDayChecklistViewController.m,它是RaceDayChecklistViewControllerBase.m的子类 在RaceDayChecklistVC.m中,添加了一个目标操作,该操作将被调用两次。nextOrNewButton是我想要调用“demo”操作的单击按钮。 此外,checklistnavigationItem是条形按钮项 - (void)viewDidLoad { checklistTableViewBase=checkli
- (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];
}
为什么要多次调用动作演示?基类是怎么负责的
请引导。您正在收听所有事件,因此您将获得所有事件。 通常要捕获按钮点击事件,我们将在内部设置UIControlEventTouchUpInside
typedef NS_选项(整数,uicontrol事件){
UIControlEventTouchDown=1我可以看到这一点
[nextOrNewButton removeTarget:self action: @selector(demo) forControlEvents:UIControlEventAllEvents];
这意味着,当您触摸按钮时,触碰
事件(UIControlEventTouchUpInside
)触发,因此它将触发一次。之后,触碰
将发生,因此它将使用UIControlEventTouchDownInside
再次触发
所以你可以用这个
[nextOrNewButton removeTarget:self action: @selector(demo) forControlEvents:UIControlEventTouchUpInside];
ui按钮
按下时生成多个事件:通常它们是UIControlEventTouchDownInside
和UIControlEventTouchUpInside
。因此,如果要处理按下操作,您应该捕获所需的事件(可能是UIControlEventTouchUpInside
),而不是UIControlEventsAll
设置uicontrolEventalEvents
这就是为什么当您触摸按钮时需要两种类型的触摸,因此您应该只使用一种类型的事件
或者UIControlEventTouchUpInside
或者uicontrolEventTouchOutside
[nextOrNewButton addTarget:self action:@selector(demo) forControlEvents: UIControlEventTouchUpInside];
不,这是我唯一使用的地方。@maniwhat是最合适的。。这是我第一次使用target。。请您引导。。我只需单击按钮并移动。@Lalit_vicky您可能会想使用UIControlEventTouchUpInside
@Lalit_vicky请您使用
[nextOrNewButton addTarget:self action:@selector(demo) forControlEvents: UIControlEventTouchUpInside];