开发者社区> 问答> 正文

使用变量语法打开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]];

展开
收起
爵霸 2016-03-24 08:33:22 1992 0
1 条回答
写回答
取消 提交回答
  • If 如何agencyWebsite是UILabel*类型,你需要访问它的text属性,不应该传递对象本身到 URLWithString:

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

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

    2019-07-17 19:11:48
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载