2017-06-20 8 views
0

セルのクリックで特定のURLを開きたいとします。テーブルビューのセルにあるURLを開く

オブジェクトを使用してこれを実行しようとしていますが、機能しません。文字列 'url'は(null)になります。

コード:

//setting object 
[self.url2 addObject:@"http://google.nl"]; 

//method to select cell 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Get your url, eg: NSString *url = yourArray[indexPath.row]; 
    NSString *url = [NSString stringWithFormat:@"%@", [self.url2 objectAtIndex:indexPath.row]]; 
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]]; 
} 

これはさえ可能ですか?

ありがとうございました。

+0

なぜNSString * url = [self.url2 objectAtIndex:indexPath.row]; ? –

+0

"不正なアクセス"を返す原因 - エラー – Anoniem

+0

すべてのコードを投稿できますか? –

答えて

1

NSMutableArray *URLArray = [[NSMutableArray alloc] init]; 
[URLArray addObject:@"http://google.nl"]; 
NSString *URLString = [NSString stringWithFormat:@"%@",[URLArray objectAtIndex:indexPath.row]]; 
NSLog(@"URLString: %@",URLString.description); 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:URLString] options:@{} completionHandler:nil]; 

あなたはNSMutableArrayURLsを追加したが URLArray.count == numberOfRowsInSectionNSRangeExceptionを避けるためにいることを確認することができ、このスニペットを試してみてください。

+1

最新のコメントを見る固定されている – Anoniem

関連する問題