UIControl: sendActionsForControlEvents omits UIEvent

我怕爱的太早我们不能终老 提交于 2019-11-29 07:37:09

I would assume that this is because the sendActionsForControlEvents: method can't know which UIEvent (if any) your control event should be associated with.

You could try to send all the actions separately (replicating what the sendActionsForControlEvents: method does, according to the documentation), so you can specifically associate them with a UIEvent:

UIEvent *event = ...;
UIControlEvents controlEvent = ...;

for (id target in [self allTargets]) {
    NSArray *actions = [self actionsForTarget:target forControlEvent:controlEvent];
    for (NSString *action in actions) {
        [self sendAction:NSSelectorFromString(action) to:target forEvent:event];
    }
}

I have ONE possible solution at the moment, but I'm not very happy about it. For others faced with the same problem though, here it is. First, declare a local variable or property for a UIEvent thus:

@property (nonatomic, assign) UIEvent * currentEvent;

Now, in your touch-handling routines, set that local variable to the current UIEvent for that routine before calling [self sendActionsForControlEvents:] like so, replacing UIControlEventTouchDown with whichever action you want to send out of course.

self.currentEvent = event;
[self sendActionsForControlEvents: UIControlEventTouchDown];

Finally, override the following method thus:

- (void) sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
{
    [super sendAction:action to:target forEvent:self.currentEvent];
}

This works, but I am not in the least bit fond of it, so if anybody has an alternative solution that doesn't rely on holding a weak reference to a UIEvent, I will be overjoyed to hear it!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!