我试图实现的是:当我点击一个特定的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");
我想不出发生了什么事。为什么我不能将字符串传递给私有方法?
发布于 2014-12-07 23:43:56
该操作应该只是一个方法的选择器,该方法的签名必须是“接受单个id参数并返回void的方法”。id参数(通常)将是发送消息的对象。
如果需要,目标(向其发送操作的对象)可以使用发送方参数提取附加信息,但需要请求该附加信息。它不是免费提供的。
也就是说,您的ImageView子类可能有以下方法:
- (void)setPhoneNumber:(NSString *)phoneNumber; // set a phoneNumber property
- (void)prepareToBeTapped
{
UITapGestureRecognizer *tapFirstGuy = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(makeCallToThisPerson:)];
[self addGestureRecognizer:tapFirstGuy];
}
- (void)makeCallToThisPerson:(id)sender
{
NSString *phoneURL = [NSString stringWithFormat:@"tel:%@", phoneNumber];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneURL]];
}
也就是说,知道电话号码的不是行动,甚至不是UITapGestureRecognizer
。目标必须以其他方式知道(或能够获得)电话号码,也许将其作为可设置的属性。
发布于 2014-12-08 00:20:21
您还可以尝试编写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
中传递更多的数据。
发布于 2014-12-07 23:14:56
这是错误的,它不是一个有效的选择器。
UITapGestureRecognizer *tapFirstGuy = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(makeCallToThisPerson:@"1234567")];
你需要改变到这一点:
UITapGestureRecognizer *tapFirstGuy = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(makeCallToThisPerson:)];
在您的方法中,参数将是tapGesture,就是这样:
- (void)makeCallToThisPerson:(UITapGestureRecognizer *)tapGesture
您可以做几件事,以便pas参数:
1.-使用imageView的de tag,tapGesture知道它附加的视图,您可以使用标记视图:
UITapGestureRecognizer *tapFirstGuy = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(makeCallToThisPerson:)];
imageViewOne.tag = 1234567
[imageViewOne addGestureRecognizer:tapFirstGuy];
- (void)makeCallToThisPerson:(UITapGestureRecognizer *)tapGesture
{
NSString *phoneNum = [NSString stringWithFormat:@"tel:%ld",tapGesture.view.tag];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNum]];
}
其他选项,子类以将NSString附加到imageView作为新属性。
https://stackoverflow.com/questions/27348917
复制相似问题