-1

I am newbie too ios and objective c,I know my question is very simple but i found not solution do asked,I am having a method for a static value,now i want to make it dynamic by passing value to that method,So can anybuddy please tell me how to write method with parameter and how to call it.my code is as below:

-(void)phoneNumberLabelTap
{
    NSURL *phoneUrl = [NSURL URLWithString:[NSString stringWithFormat:@"telprompt:%@",fonlabel.text]];

    if ([[UIApplication sharedApplication] canOpenURL:phoneUrl]) {
        [[UIApplication sharedApplication] openURL:phoneUrl];
    } else {
        UIAlertView * calert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Call facility is not available!!!" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        [calert show];
    }
}

I want to add a parameter to this method, And i am calling this method as below,

fonlabel.userInteractionEnabled = YES;
    homeLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture =
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(phoneNumberLabelTap)];
    [fonlabel addGestureRecognizer:tapGesture];
    [homeLabel addGestureRecognizer:tapGesture];
net test
  • 21
  • 3

3 Answers3

1
- (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]];
}
Rahul Patel
  • 1,822
  • 12
  • 21
0

Instead of giving a UILabel, you can give a UIButton and can give the same method as target for the button

UIButton *fonButton = [[UIButton alloc] init];
[fonButton addTarget:self action:@selector(phoneNumberButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

and in the target method like this

-(void)phoneNumberButtonClicked:(UIButton *)sender
{
    NSURL *phoneUrl = [NSURL URLWithString:[NSString stringWithFormat:@"telprompt:%@",sender.titleLabel.text]];

    if ([[UIApplication sharedApplication] canOpenURL:phoneUrl]) {
        [[UIApplication sharedApplication] openURL:phoneUrl];
    } else {
        UIAlertView * calert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Call facility is not available!!!" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        [calert show];
    }
}
Arun
  • 1,391
  • 1
  • 10
  • 29
0

One gesture can be applied to one object. You need to create 2 gestures for both labels. Below code with small changes will resolve your problem.

-(void)phoneNumberLabelTap:(UITapGestureRecognizer*)tapRecognizer
{
    UILabel* lblPhone = (UILabel*)tapRecognizer.view;

    NSURL *phoneUrl = [NSURL URLWithString:[NSString stringWithFormat:@"telprompt:%@",lblPhone.text]];

    if ([[UIApplication sharedApplication] canOpenURL:phoneUrl]) {
        [[UIApplication sharedApplication] openURL:phoneUrl];
    } else {
        UIAlertView * calert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Call facility is not available!!!" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        [calert show];
    }
}

    fonlabel.userInteractionEnabled = YES;
    homeLabel.userInteractionEnabled = YES;

    [homeLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(phoneNumberLabelTap:)]];
    [fonlabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(phoneNumberLabelTap:)]];
bunty
  • 629
  • 5
  • 8
  • Thing is method takes static value of "lblphone",i want to ass value whichever label clicked.hope you are getting me – net test Dec 28 '15 at 13:39
  • Yes, I'm getting. You can see I changed that value to make it dynamic from "fonlable.text" to "lblPhone.text". You will get the value in "lblPhone" from lable whichever clicked. – bunty Dec 28 '15 at 13:41