0

我会很快。我有 6 张图片,附有 6 个手势和一个 IBAction。我希望每个手势都将参数传递给动作,所以我不必编写 6 个单独的动作。这是我的代码:

    oneImage =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"one.gif"]];
    two Image=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"two.gif"]];
    +4 more images

     UITapGestureRecognizer *oneGest=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(insertChar:)];
         UITapGestureRecognizer *twoGest=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(insertChar:)];
    +4 more gestures


    -(IBAction)insertChar:(id)sender
    {

    textfield.text = [textfield.text stringByAppendingString:@" PASS HERE VALUE FROM GESTURE,"ONE","TWO",etc "];
    }
4

2 回答 2

1

无法将任意数据传递给该insertChar:方法。将sender是手势识别器。这是一种可能的解决方案:

oneImage =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"one.gif"]];
oneImage.tag = 1;
twoImage=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"two.gif"]];
twoImage.tag = 2;
// +4 more images

UITapGestureRecognizer *oneGest=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(insertChar:)];
UITapGestureRecognizer *twoGest=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(insertChar:)];
// +4 more gestures

-(IBAction)insertChar:(UITapGestureRecognizer *)sender {
    static NSString *labels[6] = { @"ONE", @"TWO", ... @"SIX" };
    UIView *view = sender.view;
    NSInteger tag = view.tag;
    NSString *label = labels[tag - 1]; // since tag is 1-based.

    textfield.text = [textfield.text stringByAppendingString:label];
}
于 2013-03-20T16:48:23.477 回答
0

您需要以某种方式将您作为参数获得的“发送者”值-(IBAction)insertChar:(id)senderUIImageView您创建的 s 联系起来。

操作方法将如下所示:

-(IBAction)insertChar:(id)sender
{

 UIGestureRecognizer *gestureRecognizer = (UIGestureRecognizer*)sender;
 UIView *view = gestureRecognizer.view;
//do whatever you want with the view that has the gestureRecgonizer's event on

}

然后,您可以以不同的方式链接您的视图。一种方法是使用标签属性。

于 2013-03-20T16:48:13.507 回答