设置为在ios中调用多个标签

时间:2013-03-08 14:29:07

标签: ios objective-c

我有一个电话号码agency.phone的变量,我有一个标签,该号码在视图agencyPhone

上显示

我希望能够点击标签来拨打该号码,我找到了这段代码但不确定如何在我的情况下实现它:

NSString *phoneNumber = [@"tel://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];

感谢你帮助了一个noobie!

4 个答案:

答案 0 :(得分:7)

在IB中创建标签,然后在.m文件中添加此代码

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        UITapGestureRecognizer* phone1LblGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(phone1LblTapped)];
        // if labelView is not set userInteractionEnabled, you must do so
        [agencyPhone setUserInteractionEnabled:YES];
        [agencyPhone addGestureRecognizer:phone1LblGesture];
    }

    - (void)phone1LblTapped
    {
        UIDevice *device = [UIDevice currentDevice];
        if ([[device model] isEqualToString:@"iPhone"] ) {
            NSString *phoneNumber = [@"tel://" stringByAppendingString:agencyPhone.text];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
        } else {
            UIAlertView *Notpermitted=[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Your device doesn't support this feature." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [Notpermitted show];
        }
    }

答案 1 :(得分:4)

您应该将标签切换为按钮并添加您按下按钮时要执行的代码。

例如:

- (void)callPhoneNumber:(id)sender {
  NSString *phoneNumber = [@"tel://" stringByAppendingString:mymobileNoButton.titleLabel.text];
  [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
}

答案 2 :(得分:1)

您需要使用UITapGestureRecognizerUILabel点击

UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourClickMethod:)];
[titleLabel setUserInteractionEnabled:YES];
[titleLabel addGestureRecognizer:gesture];

-(voidyourClickMethod{
NSString *phoneNumber = [@"tel://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
}

确保设置userInteractionEnabled = YES

的属性UILabel

答案 3 :(得分:1)

您可以使用touchesBegan

来处理UILabel
-(void)touchesBegan:(NSSet *) touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint point = [touch locationInView:self.view];
    CGRect labelRect = yourLabel.frame;
    if( CGRectContainsPoint (labelRect, point))
    {
       //label was clicked
       //Add phone number handling code here
    }  

}