用ios写一个带参数的方法

我也是新手太ios和客观c,我知道我的问题是非常简单的,但我发现没有解决scheme做问,我有一个静态值的方法,现在我想通过传递值的方法使它dynamic,所以可以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]; 

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

你可以给一个UIButton ,而不是给一个UILabel ,并且可以给出与button目标相同的方法

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

一个手势可以应用于一个对象。 您需要为两个标签创build两个手势。 下面的代码稍作修改就可以解决你的问题。

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