3

I want to have a single method for all the buttons (and all their events) of a view. So far this is what I've got:

- (IBAction)uibuttonEvent:(id)idSender forEvent:(UIEvent*)uieventHandle
{
    if (idSender == [self uibuttonConnectInput]) {
        if ([uieventHandle type] == UIEventTypeTouches) {
            [[self uibuttonConnectInput] setTitle:@"did it!" forState:UIControlStateNormal];
        }

    } else if (idSender == [self uibuttonSomething]) {
        ...

    }
}

So now I can detect whether the event is a UIEventTypeTouches or not, but I want to check more specifically whether it is e.g. UIControlEventTouchUpInside - how can I do that?

2 Answers2

1

As discussed here the UIEvent that you get from UIButton doesn't give you any information about which UIControlEvent caused it. Either register separate methods for each event type (the recommended solution) or make your own subclass of UIControl and make it behave differently.

Community
  • 1
  • 1
Martin Gjaldbaek
  • 2,987
  • 4
  • 20
  • 29
  • I already know that - but I want a single method for each event (not a separate one). –  Dec 16 '11 at 10:38
  • Edited my answer to reflect this. I still don't understand why you prefer a switch over separate methods (which is Apple's suggested way of doing it) but in any case, here it is :) – Martin Gjaldbaek Dec 16 '11 at 12:23
  • You forgot the important part that I can't get to work: The '// switch...' or an '} else if (...) {'. I just don't know how to formulate the 'if (... == UIControlEventTouchUpInside) {' part. You just wrote the same thing I've got in my question. –  Dec 16 '11 at 14:26
  • Thanks, that's what I was looking for! Now I know that there is no easy way to do what I want and I'll go with a separate method for each event type. –  Dec 16 '11 at 19:39
0

I think you have to give different tag for all that button.

- (IBAction)uibuttonEvent:(id)idSender {
    int iTag =  [sender tag];
}

You can identify which button was pressed.

All the best!!!

WrightsCS
  • 50,551
  • 22
  • 134
  • 186
Nishith Shah
  • 523
  • 3
  • 16
  • 1
    I think he's telling about defining an event, which occurred to this button, he's already able to know which button he's working with – makaron Dec 16 '11 at 10:08
  • As makaron mentioned, I am already able to detect the UIButton, but not the specific event. –  Dec 16 '11 at 10:14