【问题标题】:Linking Action to Url将操作链接到 URL
【发布时间】:2011-12-29 00:19:58
【问题描述】:

好的,我很迟钝,所以我需要明确的说明如何将底部的 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,所以对我来说轻松一点。

【问题讨论】:

  • 能否请您重新表述您的问题?你到底想做什么?打开 URL onViewLoad 还是通过点击标签打开 URL?
  • 或者您想通过点击按钮拨打电话?
  • 是的,通过点击标签打开网址。对不起。
  • 将标签作为链接是理想的

标签: objective-c cocoa-touch iphone-sdk-3.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 使 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]];
}

【讨论】:

  • 好的,我把代码改成了上面的...我将 IBAction 添加到我的头文件中,在我的号码旁边添加了一个按钮,上面写着“呼叫”,然后将 IBAction 指向该按钮.当我运行它并单击按钮时,什么也没有发生。没有错误。
  • 将 NSLog 放入 IBAction 并检查它是否被调用...如果是,则在传递给 openURL 之前比 NSLog'number' 变量并检查打印出来的内容...尝试使用随机数我在编辑部分给出的硬编码。
  • 是的,当我做 NSLog(number);每次我单击它时,数字都会显示在控制台中。就是什么都不做。
  • 你在模拟器上试试吗?因为在模拟器上它不会工作...... :) 因为上面没有电话......你必须在真实设备上测试它
  • 是的,伙计,我喜欢 Android 开发,这简直是地狱……尤其是在 hackintosh 上
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-02
  • 2012-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多