有自定義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]];
}
}