gpt4 book ai didi

ios - Objective-C:将字符串参数传递给 taggesture @selector

转载 作者:可可西里 更新时间:2023-11-01 05:08:35 24 4
gpt4 key购买 nike

我想要实现的是:当我点击一个特定的 UIImageView 时,UITapGesture 会将一个字符串传递给 tap 方法。

我的代码如下:假设我已经有一个 UIImageView 对象,当我点击这个图像时,它会调用电话,

UITapGestureRecognizer *tapFirstGuy = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(makeCallToThisPerson:@"1234567")];
[imageViewOne addGestureRecognizer:tapFirstGuy];

- (void)makeCallToThisPerson:(NSString *)phoneNumber
{
NSString *phoneNum = [NSString stringWithFormat:@"tel:%@", phoneNumber];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNum]];
}

但是,我收到以下编译错误:@selector(makeCallToThisPerson:@"1234567");

我不知道发生了什么。为什么我不能将字符串传递给私有(private)方法?

最佳答案

还有一种方法可以尝试编写 UITapGestureRecognizer 的子类,如下所示:-

@interface customTapGestureRecognizer : UITapGestureRecognizer

@property (nonatomic, strong) NSString * phoneNumber;

@end


// customTapGestureRecognizer.m

@implementation customTapGestureRecognizer

@end


// =====================

....

customTapGestureRecognizer *singleTap = [[customTapGestureRecognizer alloc] initWithTarget:self action:@selector(makeCallToThisPerson:)];

singleTap.phoneNumber = @"1234567";


-(void) makeCallToThisPerson:(UITapGestureRecognizer *)tapRecognizer {

customTapGestureRecognizer *tap = (customTapGestureRecognizer *)tapRecognizer;

NSLog(@"phoneNumber : %@", tap.phoneNumber);

}

注意:- 编写子类的优点是您可以在 UITapGestureRecognizer 中轻松传递更多数据。

关于ios - Objective-C:将字符串参数传递给 taggesture @selector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27348917/

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