如何在NavigationController中推送WebView

时间:2009-11-15 05:01:22

标签: iphone objective-c cocoa-touch

我想在选择表格单元格时显示网页视图

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  [uiWebView loadRequest:[NSURL URLWithString:@"http://www.google.com"]];

  [self.navigationController pushNavigationItem:uiWebView animated:YES];
}

日志

-[UINavigationController pushNavigationItem:animated:]: unrecognized selector sent to instance

3 个答案:

答案 0 :(得分:14)

上面的代码不起作用,也有一些泄漏。 -

这是修改后的版本。

UIViewController *webViewController = [[[UIViewController alloc] 
    init] autorelease];

UIWebView *uiWebView = [[[UIWebView alloc] 
    initWithFrame: CGRectMake(0,0,320,480)] autorelease];
[uiWebView loadRequest:[NSURLRequest 
    requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];

[webViewController.view addSubview: uiWebView];

[self.navigationController 
    pushViewController:webViewController animated:YES];

答案 1 :(得分:4)

你走了:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  UIViewController *webViewController = [[UIViewController alloc] init];

  UIWebView *uiWebView = [[UIWebView alloc] initWithFrame: CGRectMake(0,0,320,480)];
  [uiWebView loadRequest:[NSURLRequest requestWithURL:
                             [NSURL URLWithString: @"http://www.google.com"]]];

  [webViewController.view addSubview: uiWebView];
  [uiWebView release];

  [self.navigationController pushViewController: webViewController animated:YES];
}

答案 2 :(得分:0)

如果你想推新的视图控制器,你需要使用 - [UINavigationController pushViewController:animated:]。要显示Web视图,您需要创建一个包含Web视图的新视图控制器,然后将其推送。