2011-12-29 26 views
1

OK,我是弱智,所以我需要明确的指示,如何在底部的标签点击链接NSURL代码段拨打电话:联作用,以URL

- (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与出路搞乱无济于事。我是否需要在我的头文件中放置一个IBAction,并以某种方式将它链接到标签?第三天编码目标c和I-OS对我来说很简单。

+0

能不能请你改一下你的问题?你到底想做什么?通过单击标签打开URL onViewLoad或打开URL? – doNotCheckMyBlog 2011-12-29 01:01:28

+0

或者您想通过点击按钮拨打电话? – doNotCheckMyBlog 2011-12-29 01:03:30

+0

是的,点击标签打开网址。抱歉。 – savagenoob 2011-12-29 01:03:43

回答

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使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]]; 
} 
+0

好的,我将代码更改为上述内容......我将IBAction添加到我的头文件中,在我的号码旁边添加了一个按钮,指出“call”,然后将IBAction指向按钮。当我运行它并单击按钮时不会发生任何事情。没有错误。 – savagenoob 2011-12-29 01:28:44

+0

把一个NSLog放进IBAction,并检查它是否被调用...如果是,那么在传递给openURL之前,检查是否打印出来...尝试使用硬编码的随机数,就像我在编辑中给出的那样部分。 – doNotCheckMyBlog 2011-12-29 01:36:56

+0

是的,当我做NSLog(号码);每当我点击它时,数字显示在控制台中。只是没有做任何事情。 – savagenoob 2011-12-29 01:41:21