有自定义UITableViewCell 其中包含两枚UILabel。表格单元显示information/text。一些单元设置:
cell.myTextlabel.text = @"http://www.google.de"
我想在点击这些text链接时,safari浏览器可以打开网页。应该怎么实现?
有自定义UITableViewCell 其中包含两枚UILabel。表格单元显示information/text。一些单元设置:
cell.myTextlabel.text = @"http://www.google.de"
我想在点击这些text链接时,safari浏览器可以打开网页。应该怎么实现?
设置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]];
}
}