Html没有正确加载

时间:2011-10-24 11:10:44

标签: iphone html objective-c ios uiwebview

我有10个html存储在一个数组中...我想通过按钮点击显示每个HTML ...但我的应用程序崩溃..这是代码..

int xpos=10,ypos=10;

for (int i=0; i<[array count]; i++) {

    UIButton *but=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [but setTag:i];
    but.backgroundColor=[UIColor redColor];
    but.frame=CGRectMake(xpos, ypos, 50, 50);
    xpos+=90;

    [self.view addSubview:but];
    [but addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
}


- (void)buttonClicked:(UIButton *)sender {

    NSString *str=[array objectAtIndex:sender.tag];
    [webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:str ofType:@"html"]isDirectory:NO]]];
    [webview release];
}

如何克服这个问题?这是崩溃报告

24/10/11 4:56:08 PM加载HTML [4655] *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'* - [NSURL initFileURLWithPath:isDirectory:]: nil字符串参数'

3 个答案:

答案 0 :(得分:1)

您要初始化网页浏览的位置?

请删除[webview release];并立即尝试。

答案 1 :(得分:0)

24/10/11 4:56:08 PM Loading HTML[4655] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:isDirectory:]: nil string parameter'

您的错误日志表明您为值 str 发送了nil。

请检查str的值。这应该解决问题。可能是你拼错了文件的名字。


更新评论

尝试在那里替换您的文件名,而不是在运行时获取它。

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"yourHtmlFileNameGoesHere" ofType:@"html"]isDirectory:NO]]];

如果上述情况有效,那么您应该检查数组。

答案 2 :(得分:0)

看起来[[NSBundle mainBundle] pathForResource:str ofType:@"html"]的结果为nil - 这意味着现在可以在主包中找到该文件。确保存在具有该名称和扩展名的文件。请记住,文件名区分大小写。

相关问题