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条)

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏