NSInvalidArgumentException',原因:"配置不能为零",从UIWebView迁移到WKWebView。



我似乎找不出什么是零。调用此WKWebView的控制器不是nil。一定有某种类型的配置我没有设置。有什么想法吗?

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);
}

我无法通过添加WKWebViewIBOutlet在模拟器上重现错误。

但是,如果我放置一个UIWebView对象,并将其在Storyboard中的类更改为WKWebView,我确实会复制它
如果这就是你对"更新你的代码";,请不要那样做
相反,请删除Storyboard中的出口,并添加一个新的WKWebViewWKWebView&CCD_ 6是两个不同的类;目标;是一样的。但它们是不同的,需要不同的初始化/参数,当从Storyboard读取时,当加载发生时,它会破坏这种情况下的一切:

如果你打开一个新添加的WKWebView出口的故事板(作为源代码,即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>

如果您手动将UIWebView的类更改为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(,则设置为自定义WKWebViewBUT,并且有一个BUT,如果您仔细检查正确的BUT(前一个(,您会看到它是wkWebViewConfiguration标签。故事发生在故事板上。它是在添加出口时隐式添加的。

尽管Apple文档中说可以创建WKWebView作为出口,但我还没有看到这样的例子。我建议将其从xib/storyboard中删除,使其成为常规实例变量,并使用代码内初始化。

(在我的情况下,当它被添加为子视图时,这也需要添加约束。(

最新更新