2017-07-25 51 views
-1

我有一个主表和详细视图控制器。详细信息VC通过Web视图显示URL。IOS/Objective-C:在详细视图控制器中取消分配web视图

如果使用主/细节设置的内置导航,如果我返回然后按其他表格单元格,则详细视图将更新Web视图。无论我使用UIWebView还是WKWebView都是如此。

但是,在详细视图中,无需返回表格,我想让用户可以通过相同的Web视图查看多个网址。我正在尝试在viewwillappear中通过更改请求的url来完成此操作。但是,UIWebView(或者WKWebView)会继续显示相同的网址。我似乎无法摆脱第一个网址。

我试过各种方法来关闭或停止现有的网络视图,清除缓存,删除cookies等重新加载之前,但仍然看到相同的网址。将不胜感激任何建议。这里是我的代码和一些我已经试过的东西不工作:

 -(void) changeURL: (NSNumber*)param { 
     NSURL *testurl1=[NSURL URLWithString:@"http://www.apple.com"]; 
     NSURL *testurl2 =[NSURL URLWithString:@"http://www.google.com"]; 
     //if param is 1 { 
     _urlToLoad = testurl1;} 
     else { 
     _urlToLoad = testurl2;} 
     } 
Edit: Method below is updateInterface, not viewWillAppear 
    -(void) updateInterface { 
     UIWebView *webView; 
     NSURLRequest *request=[NSURLRequest requestWithURL:_urlToLoad]; 
      webView = nil;//This blocks the web view from loading at all for some reason I can't understand. Setting wkWebView to nil does not block loading. 
     [webView stopLoading];//no effect 
     [webView loadRequest:request]; 
     [[NSURLCache sharedURLCache] removeCachedResponseForRequest:request]; 
      [[NSURLCache sharedURLCache] removeAllCachedResponses]; 
      NSString *myDomain = @"www.google.com"; 
      for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) { 

       if([[cookie domain] isEqualToString:myDomain]) { 

        [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie]; 
       } 
      } 
    //wkwebview version 
      WKWebView *wkwebView; 
      WKWebViewConfiguration *wkConfig = [[WKWebViewConfiguration alloc] init]; 
      wkwebView = nil; 
      wkwebView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:wkConfig]; 
      wkwebView.navigationDelegate = self; 
      [wkwebView loadRequest:request]; 

      [webView addSubview:wkwebView]; 

      CGRect webFrame = [[UIScreen mainScreen] applicationFrame]; 
      webFrame.origin.y = 100.0; // statusbar 
      webFrame.origin.y -= 100.0; // statusbar 20 px cut from y of frame 
      webView = [[UIWebView alloc] initWithFrame:webFrame]; 
      } 
+0

这是一个有点混乱......如果你注释掉** **所有在'viewWillAppear'你的代码,你会得到一个“细节VC”具有Web视图和它加载一个URL?如果是这样,那么设置*那个* URL的代码在哪里? – DonMag

+0

是的我可以加载一个Web视图罚款第一次与[WebView的loadRequest:request1];或者[wkwebView loadRequest:request1]。还有另一种方法没有显示指定request2。一旦request1加载完成,我无法加载request2。卡住request1; – user6631314

+0

我编辑的问题,使这个更清晰 – user6631314

回答

1

UIViewController方法“viewWillAppear中”在Objective-C的以下特征:

- (void)viewWillAppear:(BOOL)animated 

,你没有任何争论地实施它。

结果是,它不被视为超类UIViewController中定义的原始方法的重载,而是被视为由您的子类引入的全新自定义方法;除非你在某处明确地调用它,否则它将永远不会执行。

来源:SDK Reference

+0

你是对的。 – user6631314

相关问题