【问题标题】:Load URLs into UIWebView from NSArray?从 NSArray 将 URL 加载到 UIWebView 中?
【发布时间】:2012-06-23 13:25:36
【问题描述】:

我用导航控制器创建了一个表格视图。 我想将表格数据与UIWebView 链接起来,这样当我点击特定的表格数据时,它将打开其各自的 URL。

我该怎么做?

【问题讨论】:

  • -1 -- 看起来你在提问之前没有对这个问题进行任何研究;还有,whathaveyoutried.com??

标签: iphone objective-c xcode ios5


【解决方案1】:

我假设您有 URL 数组

   NSArray* aryURL =[[NSURL alloc]initWithObjects:@"WWW.google.com",@"www.yahoo.com",@"www.facebook.com",nil];

现在在 tableView DataSource 方法中

  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {
       return [aryURL count];
  }


  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
     if (cell == nil) 
     {
          cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
     }
     cell.textLabel.text = [aryURL objectAtIndex:indexPath.row];
     return cell;
 }


 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
     WebViewController *webObj = [[WebViewController alloc]initWithNibName:@"WebViewController" bundle:nil];
     webObj.stringURl = [aryURL objectAtIndex:indexPath.row];
     [self.navigationController pushViewController:webObj animated:YES];
 }

现在在 WebViewController 类中,您必须创建 stingURL 文件和 @property @synthesise 它,在 ViewDidLoad 中,您可以将 stringURL 提供给 WebView 对象作为 URL 字符串。

【讨论】:

  • -(void) initUIWebView { CGRect webframe=CGRectMake(0.0, 0.0,320.0, 450.0); UIWebView webview=[[UIWebView alloc]initWithFrame:webframe]; [webview1 setDelegate:self]; NSArray urla =[[NSURL alloc]initWithObjects:@"WWW.google.com",@"www.fb.com",nil]; NSURL *url=[NSURL URLWithString:urlas]; NSURLRequest *requestobj=[NSURLRequest requestWithURL:url]; [webview1 loadRequest:requestobj]; [self.view addSubview:webview1];我使用了这段代码,但由于 NSURL 未声明,url 行数组中出现错误
  • 以及如何创建StringURL文件??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-09
  • 2013-04-12
  • 2013-11-30
  • 1970-01-01
  • 2011-01-28
  • 2011-08-04
  • 2011-09-12
相关资源
最近更新 更多