【问题标题】:Write a method with argument in ios [duplicate]在ios中编写一个带参数的方法[重复]
【发布时间】:2016-04-02 07:50:10
【问题描述】:

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

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

【问题讨论】:

标签: ios objective-c methods


【解决方案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]];
}

【讨论】:

  • 我对@9​​87654322@这一行感到困惑。data 是什么?
  • 我改了答案试试这个
【解决方案2】:

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

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

【讨论】:

  • 嗨,arjun 感谢您的回答,但我想将字符串值传递给它,
【解决方案3】:

一个手势可以应用于一个对象。您需要为两个标签创建 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:)]];

【讨论】:

  • 事情是方法采用“lblphone”的静态值,我想为点击的标签赋值。希望你得到我
  • 是的,我知道了。您可以看到我更改了该值以使其从“fonlable.text”变为“lblPhone.text”。您将从标签中获得“lblPhone”中的值,无论单击哪个。
猜你喜欢
  • 1970-01-01
  • 2010-12-13
  • 2021-10-26
  • 1970-01-01
  • 2014-03-24
  • 2020-07-24
  • 2013-08-15
  • 2023-03-18
  • 1970-01-01
相关资源
最近更新 更多