tags:

views:

197

answers:

2

The UIControl documentation clearly states:

When a user touches the control in a way that corresponds to one or more specified events, UIControl sends itself sendActionsForControlEvents:.

So what I did is pretty simple. I created a UIControl subclass, and overrided this, like this:

- (void)sendActionsForControlEvents:(UIControlEvents)controlEvents {
    [super sendActionsForControlEvents:controlEvents];
    NSLog(@"- (void)sendActionsForControlEvents:(UIControlEvents)controlEvents");
}

Then I instantiated my custom UIControl and added a action-target-thing like this:

MyCustomControl *twb = [[MyCustomControl alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 80.0f, 80.0f)];
twb.backgroundColor = [UIColor yellowColor];
[twb addTarget:self action:@selector(anyTouch:) forControlEvents:UIControlEventAllTouchEvents];
[self addSubview:twb];

also, I implemented that -anyTouch: method for the selector, like this:

- (void) anyTouch:(id)sender {
    NSLog(@"anyTouch");
}

What happens: I touch the view from that control, and -anyTouch throws out the "anyTouch" log messages while I am touching around on it. But even though I subclassed UIControl and overrided that sendActionsForControlEvents:, I don't get the log message like I should. That makes no sense. I've overwritten it. It should log that message, damn it. %!§$%

A: 

I ran into the same problem. I don't think UIControl itself uses sendActionsForControlEvents: to dispatch sendAction:to:forEvent:. From what little code examples I've seen this function is there for outside users who need to push the events to registered targets.

MihaiD
A: 

This is weird, because the UIControl docs say:

When a user touches the control in a way that corresponds to one or more specified events, UIControl sends itself sendActionsForControlEvents:. This results in UIControl sending the action to UIApplication in a sendAction:to:from:forEvent: message.

It sounds like we should be able to override sendActionsForControlEvents:, yet my overridden method never gets called either.

jbenet