What I am trying to achieve is this: when I tap an specific UIImageView, the UITapGesture will pass a string into the tap method.
My code follows below: Imagine I have an UIImageView object there already, and when I tap this image, it will make a phone call,
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]];
}
However, I am getting the following compile error: @selector(makeCallToThisPerson:@"1234567");
I cannot figure what is happening. Why can't I pass string to the private method?
The action should be just the selector for a method whose signature must be "method that takes a single id parameter and returns void". The id parameter will (typically) be the object sending the message.
The target (the object the action is sent to) can use the sender parameter to extract additional information if needed, but that additional information needs to be asked for. It isn't supplied gratis.
That is, your ImageView subclass might have methods like:
- (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]];
}
That is, it's not the action nor even the UITapGestureRecognizer
that knows the phone number. The target must know (or be able to obtain) the phone number some other way, perhaps carrying it as a settable property.