gpt4 book ai didi

ios - 将值传递给 IBAction

转载 作者:行者123 更新时间:2023-11-29 04:03:40 25 4
gpt4 key购买 nike

我会很快的。我有 6 张图像,附有 6 个手势和 1 个 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 "];
}

最佳答案

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

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];
}

关于ios - 将值传递给 IBAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15529492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com