'NSInvalidArgumentException', reason: '*** -[NSURL initWithString:relativeToURL:]: nil string parameter'
下面是代码:
-(void)buttonAction:(id)sender{
webView *myView = [[webView alloc]init];
myView.url = @"http://www.baidu.com";
[self.view addSubview:myView];
[myView release];
}
webView的初始化代码:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSURL* myurl = [[NSURL alloc]initWithString:self.url];
[self loadRequest:[NSURLRequest requestWithURL:myurl]];
}
return self;
}
原因找出来了:当程序走到红色代码部分之后,不会执行myView.url = @"http://www.baidu.com",而是直接对webView初始化,因为参数没传过去,所以会出现这种错误。可惜我不知道这种逻辑之下如何解决这个问题。