将动作链接到URL

时间:2011-12-29 00:19:59

标签: objective-c cocoa-touch iphone-sdk-3.0

好的,我迟钝所以我需要明确指示如何将底部的NSURL代码部分链接到标签点击以拨打电话:

- (void)viewDidLoad {
[super viewDidLoad];
lblText.text = agencyName;
lblPhone.text = phone;
lblEmail.text = email;
lblAddress.text = agcaddress;



//Set the title of the navigation bar
self.navigationItem.title = @"Agency Info"; 
mapView=[[MKMapView alloc] initWithFrame:self.view.bounds];



}


- (IBAction)callPlaceNumber:(id)sender {
    NSString *number = [NSString stringWithFormat:@"tel://%@", phone];
    number = [number stringByReplacingOccurrencesOfString:@" "
                                                 withString:@""];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:number]];
}

我试图弄乱IBActions和Outlets无济于事。我是否需要在头文件中放置IBAction并以某种方式将其链接到标签?第3天编码目标c和I-OS对我来说很容易。

1 个答案:

答案 0 :(得分:1)

您可以通过点击UILabel来实现通话但不推荐:),方法是捕捉touchEvents并检查触摸事件是否来自呼叫标签,而不是拨打电话......有点像此,

不推荐

calLabel是头文件中的IBOutlet ......

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

    if ([touch view] == callLabel)
    {
        //Your code here which makes a call...
    }
}//In began

或触摸结束时,

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];

    if ([touch view] == callLabel)
    {
        //Your code here which makes a call...
    }
}//At the end

推荐是......

创建一个UIButton make IBAction并在其中编写你的调用逻辑......就像这样,

- (IBAction)callPlaceNumber:(id)sender {
    NSString *number = [NSString stringWithFormat:@"tel://%@", phoneNumber];
    number = [number stringByReplacingOccurrencesOfString:@" "
                                         withString:@""];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:number]];
}

希望这会有所帮助!

修改

- (IBAction)callPlaceNumber:(id)sender {
    NSString *number = [NSString stringWithFormat:@"tel://12345678"];
    number = [number stringByReplacingOccurrencesOfString:@" "
                                         withString:@""];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:number]];
}