在ios中编写带参数的方法

时间:2015-12-28 12:54:57

标签: ios objective-c methods

我也是新手ios和客观c,我知道我的问题很简单,但我发现没有解决方案问我,我有一个静态值的方法,现在我想通过将值传递给该方法使其动态化,那么anybuddy可以告诉我如何用参数编写方法以及如何调用它。我的代码如下:

-(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];
    }
}

我想为此方法添加一个参数, 我将这种方法称为如下,

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

3 个答案:

答案 0 :(得分: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]];
}

答案 1 :(得分:0)

您可以提供UILabel而不是提供UIButton,并且可以提供与按钮目标相同的方法

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

和像这样的目标方法

-(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];
    }
}

答案 2 :(得分:0)

一个手势可以应用于一个对象。您需要为两个标签创建2个手势。下面的代码稍有改动即可解决您的问题。

-(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:)]];