0

我有一个使用 UITableViewController 的应用程序,它调用内部带有 UIWebView 的 UIViewController。我的一切工作正常,除了当我单击表格单元格时,它会将我带到 UIViewController 并在里面打开相应的 weburl,但是当我返回 UITableViewController 时它不会关闭网站。它抵消了网站,所以我知道我回到了 tableview 但网站仍然可见。我错过了什么?

WebViewController.m

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

}

-(void)viewDidAppear:(BOOL)animated {
webView2.delegate = self;
webView2.backgroundColor = [UIColor clearColor];
NSString *urlAddress = self.magURL;
webURL = [NSURL URLWithString:urlAddress];

requestObj = [NSURLRequest requestWithURL:webURL];
[webView2 loadRequest:requestObj];
}

-(void)viewDidDisappear:(BOOL)animated
{
webView2.delegate = nil;
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}

TableViewController.m - (void)viewDidLoad { [super viewDidLoad];

magazineArray = [[NSMutableArray alloc] init];
[magazineArray addObject:@"Joyce Meyer"];
[magazineArray addObject:@"Perry Stone"];
[magazineArray addObject:@"Robb Thompson"];
[magazineArray addObject:@"R.W. Schambach"];
[magazineArray addObject:@"T.D. Jakes"];
[magazineArray addObject:@"Reinhard Bonnke"];
[magazineArray addObject:@"Pilot"];

self.navigationItem.title = @"Magazines";

magazineURL = [[NSMutableArray alloc] init];
[magazineURL addObject:@"http://mysolutionsmagazine.com/solutions/2013/magazine"];
[magazineURL addObject:@"http://google.com"];
[magazineURL addObject:@"http://smithmediagroup.com"];
[magazineURL addObject:@"http://smgvoices.com"];
[magazineURL addObject:@"http://facebook.com/smithmediagroup"];
[magazineURL addObject:@"http://twitter.com/smgvoices"];
[magazineURL addObject:@"http://youtube.com/smgvoices"];

MagazineURLViewController *urlController = [[MagazineURLViewController alloc] init];
urlController.urlHolder = newURLHolder;

}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath     *)indexPath
{
newURLHolder = [magazineURL objectAtIndex:indexPath.row];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(50, 100, 690, 750)];

NSURL *webURL2 = [NSURL URLWithString:newURLHolder];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:webURL2];

[webView loadRequest:requestObj];
[self.view addSubview:webView];

}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

if ([[segue identifier] isEqualToString:@"Cell2"]){
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    newURLHolder = magazineURL[indexPath.row];
    [[segue destinationViewController]setMagURL:newURLHolder];

}
}
4

4 回答 4

1

您正在将 webviewcontriller 的视图作为子视图添加到 tableviewcontroller 的视图中。它可能看起来没有任何问题,我认为这是苹果的严格禁止(iPad 是一个例外,因为它有你可以使用的 splitviewcontroller)。

当您将其添加为子视图时,您的默认视图控制器方法(viewwill/didappear 等)将不会被调用。

要么将它作为一个新的视图控制器推送,要么以模态方式呈现它,当你弹出/关闭你的 webview 控制器时,一切都会正常工作。

如果您不想以模态方式推送或呈现,只需在 tableviewController 中使用 webview,实现 webView 的委托方法,将其添加为子视图。

于 2013-01-25T17:29:53.407 回答
0

与其将 webview 添加为子视图,不如推送 webview:

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

当您想关闭 web 视图时:

[self.parentViewController.navigationController popViewControllerAnimated:YES];]
于 2013-01-25T16:44:18.177 回答
0

目前看起来您从未调用您在单元格选择上创建 UIWebView 并将其添加到当前视图的 webViewController。所以我的问题是你有没有说过 removeFormSuperview?

于 2013-01-25T16:58:36.837 回答
0

看起来您正在使用情节提要。问题似乎在于不必要的代码

tableView:didSelectRowAtIndexPath:

您不必将 Web 视图添加到当前视图中,您需要在@"Cell2"视图控制器中包含该代码。

看起来你快到了,你正在将一个 URL 传递给下一个视图控制器prepareForSegue

您应该有另一个视图控制器来读取 magURL 属性并显示该网页。

表格视图控制器中更像这样的东西:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath     *)indexPath
{
  newURLHolder = [magazineURL objectAtIndex:indexPath.row];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
  if ([[segue identifier] isEqualToString:@"Cell2"]){
      [[segue destinationViewController] setMagURL:newURLHolder];
  }
}

然后在你的新视图控制器中

- (void)viewDidLoad
{
// if webview is created in the storyboard you won't need to `init` it here. Just use [self webView]
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(50, 100, 690, 750)];

NSURL *webURL2 = [NSURL URLWithString:[self magURL]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:webURL2];

[webView loadRequest:requestObj];
[self.view addSubview:webView];
}
于 2013-01-25T16:58:38.950 回答