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?

有帮助吗?

解决方案 2

- (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.

其他提示

I would assign a tag to each of the views. That way, you can still use your switch statement to tell them apart, but without having to keep a reference to each view. E.x:

- (IBAction)tapSignature:(UISwipeGestureRecognizer *)sender
{
    NSLog(@"swiped");

    switch (sender.view.tag)
    {
        case 1:
            NSLog(@"1");
            break;

        case 2:
            NSLog(@"2");
            break;

        case 3:
            NSLog(@"3");
            break;

        default:
            NSLog(@"4");
            break;
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top