我有2个视图:带标签1的视图和带标签2的视图。
每个视图都有两个手势识别器。

CGRect tempRect=CGRectMake(0, 0, 100, 100);


for (int i=1; i<=2; i++) {
    UIView *tempView=[[UIView alloc] initWithFrame:tempRect];
    UIImageView *tempImageView=[[UIImageView alloc]initWithFrame:tempRect];
    tempImageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"%@%d%@",@"image_",i,@".png"]];
    UISwipeGestureRecognizer *tempRecognizer = [[UISwipeGestureRecognizer alloc] init];
    [tempRecognizer performSelector:@selector(SwipeRight:) withObject:[NSNumber numberWithInt:i]];
    [tempRecognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [tempView addGestureRecognizer:tempRecognizer];
    [tempView addSubview:tempImageView];
    tempView.tag=i;
    [self.view addSubview:tempView];
}


我有一个方法:管理滑动的SwipeRight。

-(void)SwipeRight:(NSNumber*)MyTag{

    int MyProgr = [MyTag intValue];

    }


传递标签的正确方法是哪种?
用我的代码,我得到一个错误:

[UISwipeGestureRecognizer SwipeRight:]: unrecognized selector sent to instance...

最佳答案

设置视图标签之前

[tempView setTag:i];
 UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(callYourMethod:)];
            swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
            [tempView addGestureRecognizer:swipeRight];


- (void)callYourMethod:(UISwipeGestureRecognizer *)recognizer
{
    if (recognizer.direction == UISwipeGestureRecognizerDirectionRight)
    {
        int tagValue = recognizer.view.tag;
        NSLog(@"Down");
    }
}

10-08 12:24