1

我有一个电话号码变量,agency.phone并且我有一个标签,该号码在视图上显示agencyPhone

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

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

感谢您帮助新手!

4

4 回答 4

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];
        }
    }
于 2013-03-08T14:36:06.760 回答
4

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

例如:

- (void)callPhoneNumber:(id)sender {
  NSString *phoneNumber = [@"tel://" stringByAppendingString:mymobileNoButton.titleLabel.text];
  [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
}
于 2013-03-08T14:32:12.203 回答
1

你需要UITapGestureRecognizer用来制作UILabel可点击的

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

于 2013-03-08T14:35:49.613 回答
1

你可以处理点击 UILabel 使用touchesBegan

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

}
于 2013-03-08T14:36:25.033 回答