yue1liang1chuan 2013-04-10 03:01 采纳率: 0%
浏览 2623
已采纳

UILable链接打开safari浏览器

有自定义UITableViewCell 其中包含两枚UILabel。表格单元显示information/text。一些单元设置:

cell.myTextlabel.text = @"http://www.google.de"

我想在点击这些text链接时,safari浏览器可以打开网页。应该怎么实现?

  • 写回答

2条回答 默认 最新

  • xinyoulingxi1_tong 2013-04-10 05:34
    关注

    设置userInteractionEnabled 为 YES 。添加一个姿势识别器:

    myLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *gestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openUrl:)];
    gestureRec.numberOfTouchesRequired = 1;
    gestureRec.numberOfTapsRequired = 1;
    [myLabel addGestureRecognizer:gestureRec];
    [gestureRec release];
    

    然后实习操作方法:

    - (void)openUrl:(id)sender
    {
        UIGestureRecognizer *rec = (UIGestureRecognizer *)sender;
    
        id hitLabel = [self.view hitTest:[rec locationInView:self.view] withEvent:UIEventTypeTouches];
    
        if ([hitLabel isKindOfClass:[UILabel class]]) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:((UILabel *)hitLabel).text]];
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题