[英]How can I determine which UIControlEvents type caused a UIEvent?
What I want to do is set up a bunch of UIControl
objects to send all events through the same handler.我想要做的是设置一堆
UIControl
对象通过同一个处理程序发送所有事件。 That handler than needs to determine what the appropriate action is based on the UIControlEvents
type that triggered.该处理程序需要根据触发的
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.据我所知,
event.type
不能以这种方式工作。 I want to avoid having a subclass of UIControl
because that is severe overkill for what I want to do.我想避免拥有
UIControl
的子类,因为这对于我想要做的事情来说太过分了。 The rest of the moving parts here aren't really that important;这里的运动部件的rest并不是那么重要; what I want is a way to check against
UIControlEvents
after the target:action:
pair is used in the following setup:我想要的是一种在以下设置中使用
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.如果有不同的方法来设置一个
target:action:
发送回UIControlEvents
,我会接受的。 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.或者,至少,在
UIControl
object之外获得访问权限,这样我就可以获得我想要的类型。
You're correct;你是对的;
UIEvent.type
does not work that way. UIEvent.type
不能那样工作。 Internally, UIControl
subclasses are basically using the sendActionsForControlEvents:
method, which means they can choose to put anything they want as the parameter.在内部,
UIControl
子类基本上使用sendActionsForControlEvents:
方法,这意味着他们可以选择将任何他们想要的东西作为参数。
As for how to know, the simple answer is to have a separate method for each control event.至于怎么知道,简单的回答就是每个控制事件都有一个单独的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.