我似乎無法找出什么是 nil 。呼叫此 WKWebView 的控制器不為零。一定有某種我沒有設定的配置。有任何想法嗎?
HelpViewViewController.h
#import <UIKit/UIKit.h>
#import <WebKit/WebKit.h>
@interface HelpViewViewController : UIViewController
< WKUIDelegate, WKNavigationDelegate>{}
@property (nonatomic, strong) IBOutlet WKWebView *webView;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *sidebarButton;
@property (assign) NSString *urlString;
@end
HelpViewViewController.m
#import "HelpViewViewController.h"
#import "SWRevealViewController.h"
#import "Functions.h"
@interface HelpViewViewController ()
@end
@implementation HelpViewViewController
@synthesize webView,urlString;
NSURL *urlToSave2 = nil;
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@" start %@",urlString);
這個 NSLog 永遠不會被呼叫。在呼叫 viewDidLoad 之前它正在崩潰。
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
webView.navigationDelegate = self;
NSURL *nsurl=[NSURL URLWithString:@"http://www.apple.com"];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webView loadRequest:nsrequest];
[self.view addSubview:webView];
NSLog(@" end %@",urlString);
}
uj5u.com熱心網友回復:
盡管 Apple 檔案說您可以創建 WKWebView 作為插座,但我還沒有看到這種作業的示例。我建議將其從您的 xib/storyboard 中洗掉,使其成為常規實體變數,并使用您的代碼內初始化。
(在我的情況下,這也需要在添加為子視圖時添加約束。)
uj5u.com熱心網友回復:
我無法通過添加WKWebViewIBOutlet 在模擬器上重現錯誤。
但是,如果我放置一個UIWebView物件并將其在 Storyboard 中的類更改為WKWebView.
如果這是您為“更新代碼”所做的,請不要這樣做。
相反,洗掉 Storyboard 中的插座,并添加一個新插座,即WKWebView.
WKWebView&UIWebView是兩個不同的類,即使它們的“目標”是相同的。但它們是不同的,需要不同的初始化/引數,當從 Storyboard 讀取它時,當加載發生時,它會破壞這種情況下的一切:
如果您打開新添加的 WKWebView 插座的 Storyboard(作為源代碼,即 XML),您將擁有:
<wkWebView contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="R9K-yX-E6A">
<rect key="frame" x="0.0" y="0.0" width="320" height="284"/>
<color key="backgroundColor" red="0.36078431370000003" green="0.38823529410000002" blue="0.4039215686" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<wkWebViewConfiguration key="configuration">
<audiovisualMediaTypes key="mediaTypesRequiringUserActionForPlayback" none="YES"/>
<wkPreferences key="preferences"/>
</wkWebViewConfiguration>
</wkWebView>
如果您將 a 的類手動更改UIWebView為 a WKWebView,您將擁有:
<webView contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="JoT-Lq-Nq2" customClass="WKWebView">
<rect key="frame" x="40" y="341" width="240" height="128"/>
<color key="backgroundColor" red="0.36078431370000003" green="0.38823529410000002" blue="0.4039215686" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</webView>
您會看到,如果 webView(即UIWebView)的真實類設定為 custom WKWebView。但是,有一個但是,如果你仔細檢查正確的一個(前一個),你會看到它是一個wkWebViewConfiguration標簽。它設定在情節提要中。添加插座時會隱式添加。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/451423.html
