在iOS中编写带有参数的方法 [英] Write a method with argument in ios

查看:150
本文介绍了在iOS中编写带有参数的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也是ios和目标c的新手,我知道我的问题很简单,但是我没有找到解决方法,我有一个静态值的方法,现在我想通过将值传递给该方法使它动态化,所以任何预算都可以告诉我如何编写带有参数的方法以及如何调用它.我的代码如下:

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

推荐答案

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

这篇关于在iOS中编写带有参数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆