首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Objective:将字符串参数传递给tap手势@selector

Objective:将字符串参数传递给tap手势@selector
EN

Stack Overflow用户
提问于 2014-12-07 23:01:47
回答 4查看 7.5K关注 0票数 4

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

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

代码语言:javascript
运行
复制
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");

我想不出发生了什么事。为什么我不能将字符串传递给私有方法?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-12-07 23:43:56

该操作应该只是一个方法的选择器,该方法的签名必须是“接受单个id参数并返回void的方法”。id参数(通常)将是发送消息的对象。

如果需要,目标(向其发送操作的对象)可以使用发送方参数提取附加信息,但需要请求该附加信息。它不是免费提供的。

也就是说,您的ImageView子类可能有以下方法:

代码语言:javascript
运行
复制
- (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。目标必须以其他方式知道(或能够获得)电话号码,也许将其作为可设置的属性。

票数 1
EN

Stack Overflow用户

发布于 2014-12-08 00:20:21

您还可以尝试编写UITapGestureRecognizer的子类,如下所示:

代码语言:javascript
运行
复制
@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中传递更多的数据。

票数 6
EN

Stack Overflow用户

发布于 2014-12-07 23:14:56

这是错误的,它不是一个有效的选择器。

代码语言:javascript
运行
复制
    UITapGestureRecognizer *tapFirstGuy = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(makeCallToThisPerson:@"1234567")];

你需要改变到这一点:

代码语言:javascript
运行
复制
 UITapGestureRecognizer *tapFirstGuy = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(makeCallToThisPerson:)];

在您的方法中,参数将是tapGesture,就是这样:

代码语言:javascript
运行
复制
 - (void)makeCallToThisPerson:(UITapGestureRecognizer *)tapGesture

您可以做几件事,以便pas参数:

1.-使用imageView的de tag,tapGesture知道它附加的视图,您可以使用标记视图:

代码语言:javascript
运行
复制
 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作为新属性。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27348917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档