0

私はiPhone開発の新人です。各セクションに 1 つの行がある 6 つのセクションを持つテーブル ビューがあり、4 番目のセクションに UILabel を追加します。この UILabel テキストは URL (www.google.com) です。このラベルをクリックしたときにサファリを開きたいのですが、サファリを開くことができません

UITableViewCell 内のハイパーリンクを使用してこのUILabel をフォローすると、サファリ Web ブラウザーを開く必要がありますか?

しかし、うまくいきません。

私のコード:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [Prep defaultBGColor];

        if(indexPath.section == 3)
        {
            self.lblWebsite = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 270, 35)];
            self.lblWebsite.backgroundColor = [UIColor clearColor];
            self.lblWebsite.text= @"www.gmail.com";
            self.lblWebsite.font =[UIFont fontWithName:@"Arial-BoldMT" size:16];
            self.lblWebsite.textAlignment = UITextAlignmentLeft;
            self.lblWebsite.userInteractionEnabled = YES;
            self.lblWebsite.textColor=[UIColor blackColor];
            [cell.contentView addSubview:self.lblWebsite];

            UITapGestureRecognizer *gestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openUrl:)];
            gestureRec.numberOfTouchesRequired = 1;
            gestureRec.numberOfTapsRequired = 1;
            [self.lblWebsite addGestureRecognizer:gestureRec];
            [gestureRec release];
       }
    }
  return Cell;
}

方法

- (void)openUrl:(id)sender
{

    UIGestureRecognizer *rec = (UIGestureRecognizer *)sender;

    id hitLabel = [self.view hitTest:[rec locationInView:self.view] withEvent:UIEventTypeTouches];

    if ([hitLabel isKindOfClass:[UILabel class]]) {
         NSLog(@"%@",((UILabel *)hitLabel).text);
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"www.google.com"]];
    }
}

ここで私の間違いは何ですか??

4

3 に答える 3

2
  [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]];  

http://「 」がありません

于 2013-04-09T10:51:23.340 に答える
2

あなたは正しくやったが、私が思うに、好きなhttp://前に使うwww.google.com

NSURL *url = [NSURL URLWithString:@"http://www.stackoverflow.com"];

if (![[UIApplication sharedApplication] openURL:url])

NSLog(@"%@%@",@"Failed to open url:",[url description]);

そして、私はそれがあなたのために働くかもしれないと思います

于 2013-04-09T10:51:31.240 に答える