我会很快。 我有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 "];
}
答案 0 :(得分: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];
}
答案 1 :(得分:0)
您需要以某种方式将-(IBAction)insertChar:(id)sender
中作为参数获得的“发件人”值与您创建的UIImageView
相关联。
动作方法如下所示:
-(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
}
然后,您可以通过不同方式链接您的视图。一种方法是使用tag属性。