DaffodilGirl 2013-04-16 01:58 采纳率: 0%
浏览 2129
已采纳

使用变量语法打开URL地址

有一个变量agencyWebsite和一个标签,应该在点击下面方法的时候打开一个网站。

- (void)website1LblTapped {
    NSURL *url = [NSURL URLWithString:self.agencyWebsite];
    [[UIApplication sharedApplication] openURL:url];
}

在编译器的警报:

Incompatible pointer types sending UILabel* to parameter of type NSString*

再点击网站应用就会崩溃。不知道应该怎么解决?请高手指点一下,谢谢。

下面是设置label点击的代码:

UITapGestureRecognizer* website1LblGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(website1LblTapped)];
    // if labelView is not set userInteractionEnabled, you must do so
    [self.agencyWebsite setUserInteractionEnabled:YES];
    [self.agencyWebsite addGestureRecognizer:website1LblGesture];

运行代码:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", self.agencyWebsite.text]];
  • 写回答

1条回答 默认 最新

  • O_1001_O 2013-04-16 06:10
    关注

    If 如何agencyWebsite是UILabel*类型,你需要访问它的text属性,不应该传递对象本身到 URLWithString:

    - (void)website1LblTapped {
    
        NSURL *url = [NSURL URLWithString:self.agencyWebsite.text];
        [[UIApplication sharedApplication] openURL:url];
    }
    

    调用 self.agencyWebsite会返回您的UILabel* 对象。同时self.agencyWebsite.text会返回包含标签text的NSString*对象。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作