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*对象。

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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?