iOS - UITapGestureRecognizer - 带参数的选择器 [英] iOS - UITapGestureRecognizer - Selector with Arguments

查看:828
本文介绍了iOS - UITapGestureRecognizer - 带参数的选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我在运行时动态地将图像添加到我的视图中。我可以同时在屏幕上显示多个图像。每个图像都是从一个对象加载的。我已经为图像添加了一个tapGestureRecongnizer,以便在点击它时调用相应的方法。

In my app I am dynamically adding images to my view at runtime. I can have multiple images on screen at the same time. Each image is loaded from an object. I have added a tapGestureRecongnizer to the image so that the appropriate method is called when I tap it.

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
    [plantImageView addGestureRecognizer:tapGesture];

我的问题是我不知道我拍了什么图像。我知道我可以调用tapGestureRecognizer.location来获取屏幕上的位置,但这对我来说并不是很好。理想情况下,我希望能够将加载图像的对象传递到点击手势。但是,似乎我只能传递选择器名称imageTapped:而不是其参数。

My problem is that I don't know what image I have tapped. I know I can call tapGestureRecognizer.location to get the location on screen but thats not really much good to me. Ideally, I'd like to be able to pass the object that the image was loaded from into the tap gesture. However, it seems that I am only able to pass in the selector name "imageTapped:" and not its arguments.

- (IBAction)imageTapped:(Plant *)plant
{
   [self performSegueWithIdentifier:@"viewPlantDetail" sender:plant];
}

有没有人知道我可以将我的对象作为参数传递给tapGestureRecongnizer或我可以处理它的任何其他方式?

Does anyone know of a way that I can pass my object as an argument into the tapGestureRecongnizer or any other way I can get a handle on it?

谢谢

Brian

推荐答案

你快到了。 UIGestureRecognizer具有视图属性。如果您为每个图像视图分配并附加手势识别器 - 就像您在代码片段中看到的那样 - 那么您的手势代码(在目标上)可能如下所示:

You're almost there. UIGestureRecognizer has a view property. If you allocate and attach a gesture recognizer to each image view - just as it appears you do in the code snippet - then your gesture code (on the target) can look like this:

- (void) imageTapped:(UITapGestureRecognizer *)gr {

  UIImageView *theTappedImageView = (UIImageView *)gr.view;
}

您提供的代码不太清楚如何关联Plant模型对象使用它的相应imageView,但它可能是这样的:

What's less clear from the code you provided is how you associate your Plant model object with it's corresponding imageView, but it could be something like this:

NSArray *myPlants;

for (i=0; i<myPlants.count; i++) {
    Plant *myPlant = [myPlants objectAtIndex:i];
    UIImage *image = [UIImage imageNamed:myPlant.imageName];  // or however you get an image from a plant
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];  // set frame, etc.

    // important bit here...
    imageView.tag = i + 32;

    [self.view addSubview:imageView];
}

现在gr代码可以执行此操作:

Now the gr code can do this:

- (void) imageTapped:(UITapGestureRecognizer *)gr {

  UIImageView *theTappedImageView = (UIImageView *)gr.view;
  NSInteger tag = theTappedImageView.tag;
  Plant *myPlant = [myPlants objectAtIndex:tag-32];
}

这篇关于iOS - UITapGestureRecognizer - 带参数的选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆