如何确定哪个UIControlEvents类型导致了UIEvent?

时间:2021-12-20 07:09:21

What I want to do is set up a bunch of UIControl objects to send all events through the same handler. That handler than needs to determine what the appropriate action is based on the UIControlEvents type that triggered.

我想要做的是设置一堆UIControl对象,通过相同的处理程序发送所有事件。该处理程序需要根据触发的UIControlEvents类型确定适当的操作。

- (void)handleEventFromControl:(id)sender withEvent:(UIEvent *)event {
    UIControl *control = sender;
    NSIndexPath *indexPath = [controlIndexPaths objectForKey:control];
    UIControlEvents controlEventType = event.type; //PROBLEM HERE

    BOOL fire = NO;
    NSNumber *eventCheck = [registeredEventsByToolkitIndex objectAtIndex:indexPath.toolkit];
    fire = ([eventCheck integerValue] & controlEventType);

    if(!fire) {
        eventCheck = [registeredEventsByControlIndex objectAtIndex:indexPath.control];
        fire = ([eventCheck integerValue] & controlEventType);
    }

    if(!fire) {
        eventCheck = [registeredEventsByIndexPath objectForKey:indexPath];
        fire = ([eventCheck integerValue] & controlEventType);
    }

    if(fire) {
        [self.delegate toolkit:self indexPath:indexPath firedEvent:event];
    }
}

As best as I can tell, event.type doesn't work this way. I want to avoid having a subclass of UIControl because that is severe overkill for what I want to do. The rest of the moving parts here aren't really that important; what I want is a way to check against UIControlEvents after the target:action: pair is used in the following setup:

尽我所知,event.type不能这样工作。我想避免使用UIControl的子类,因为这对于我想做的事情来说是严重的过度杀伤。这里剩下的活动部件并不那么重要;我想要的是在以下设置中使用target:action:pair之后检查UIControlEvents的方法:

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents

If there is a different way to setup of a target:action: that sends back the UIControlEvents, I'd take that. But I am not seeing it in the documentation. Or, at the very least, get access outside of the UIControl object such that I can get the type I want.

如果有一种不同的方式来设置目标:action:发送回UIControlEvents,我会接受它。但我没有在文档中看到它。或者,至少,在UIControl对象之外访问,以便我可以获得我想要的类型。

1 个解决方案

#1


17  

You're correct; UIEvent.type does not work that way. Internally, UIControl subclasses are basically using the sendActionsForControlEvents: method, which means they can choose to put anything they want as the parameter.

你是对的; UIEvent.type不起作用。在内部,UIControl子类基本上使用sendActionsForControlEvents:方法,这意味着他们可以选择将他们想要的任何内容作为参数。

As for how to know, the simple answer is to have a separate method for each control event.

至于如何知道,简单的答案是为每个控制事件设置一个单独的方法。

#1


17  

You're correct; UIEvent.type does not work that way. Internally, UIControl subclasses are basically using the sendActionsForControlEvents: method, which means they can choose to put anything they want as the parameter.

你是对的; UIEvent.type不起作用。在内部,UIControl子类基本上使用sendActionsForControlEvents:方法,这意味着他们可以选择将他们想要的任何内容作为参数。

As for how to know, the simple answer is to have a separate method for each control event.

至于如何知道,简单的答案是为每个控制事件设置一个单独的方法。