Search code examples
iosobjective-cuiswipegesturerecognizer

Attach UISwipeGestureRecognizer to Multiple Views


I have a View Controller with a normal View. In that view, I have 4 sub views. I need each one to react to a UISwipeGestureRecognizer. I hooked the views to the UISwipeGestureRecognizer in Interface Builder and hooked the UISwipeGestureRecognizer to an IBAction. It all works great; they all react to the UISwipeGestureRecognizer.

But, I need the action to do something different, depending on what view called the IBAction. What should I do? Here's the IBAction code:

- (IBAction)swipe:(UISwipeGestureRecognizer *)sender
{
    switch (view)
    {
        case view1:
            //do something
            break;

        case view2:
            //do something
            break;

        case view3:
            //do something
            break;

        default:
        //do something
        break;
    }
}

How should I handle this?


Solution

  • - (IBAction)swipe:(UISwipeGestureRecognizer *)sender
    {
        if (sender.view == view1) {
            //do something
        }
        if (sender.view == view2) {
            //do something
        }
        if (sender.view == view3) {
            //do something
        }
    }
    

    Don't complicate what is simple. Besides, using tags will force you to define the same tags in another nib if you want to reuse the same controller with another nib, that is bad design.