我有一個主表和詳細視圖控制器。詳細信息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];
}
這是一個有點混亂......如果你註釋掉** **所有在'viewWillAppear'你的代碼,你會得到一個「細節VC」具有Web視圖和它加載一個URL?如果是這樣,那麼設置*那個* URL的代碼在哪裏? – DonMag
是的我可以加載一個Web視圖罰款第一次與[WebView的loadRequest:request1];或者[wkwebView loadRequest:request1]。還有另一種方法沒有顯示指定request2。一旦request1加載完成,我無法加載request2。卡住request1; – user6631314
我編輯的問題,使這個更清晰 – user6631314