1

我在 Resources 文件夹中有两个本地 .html 文件。我正在尝试通过以下方式加载它们,但只加载最后一页。我究竟做错了什么?

文件 = please_wait.html

这个不行。

NSError *error;
NSString* path = [[NSBundle mainBundle] pathForResource:@"please_wait" ofType:@"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
[webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:path]];   

 //Big "do-while" loop here.  It works fine so I omitted it.

文件 = update_graph.html

这个不行

 path = [[NSBundle mainBundle] pathForResource:@"update_graph" ofType:@"html"];
 htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
 [webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:path]];

//Lots of code removed.  All works correctly and doesn't touch webview

最后一个完美地工作。谷歌显示。

string = @"http://google.com";  
NSURL *url = [NSURL URLWithString: string];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
4

1 回答 1

3

从您的评论中可以看出,您的UIWebView加载很好,但在您退出方法之前,它没有机会在屏幕上刷新自身。在方法内部设置断点并等待视图加载是不够的:您必须在 iOS 意识到它需要调用的方法之前退出该UIWebView方法drawRect

要解决此问题,请将您的方法分成三部分,A B然后C将 , 和 setUIWebView的委托 inA以调用Bon webViewDidFinishLoad:,将委托 inB以调用C

以下是如何实现这一点:从一个可以在加载完成时调用选择器的委托开始:

@interface GoToNext : NSObject <UIWebViewDelegate> {
    id __weak target;
    SEL next;
}
-(id)initWithTarget:(id)target andNext:(SEL)next;
-(void)webViewDidFinishLoad:(UIWebView *)webView;
@end

@implementation GoNext
-(id)initWithTarget:(id)_target andNext:(SEL)_next {
    self = [super init];
    if (self) {
        target = _target;
        next = _next;
    }
    return self;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    [target performSelector:next];
    #pragma clang diagnostic pop
}
@end

现在将您的方法分成三个部分 - 加载第一页,加载第二页和加载第三页:

-(void)loadPleaseWait {
    NSError *error;
    NSString* path = [[NSBundle mainBundle] pathForResource:@"please_wait" ofType:@"html"];
    NSString* htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
    webView.delegate = [[GoToNext alloc] initWithTarget:self andNext:@selector(loadUpdateGraph)];
    [webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:path]];
    // big do-while loop
}

-(void)loadUpdateGraph {
    NSError *error;
    NSString* path = [[NSBundle mainBundle] pathForResource:@"update_graph" ofType:@"html"];
    NSString* htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
    webView.delegate = [[GoToNext alloc] initWithTarget:self andNext:@selector(loadGoogle)];
    [webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:path]];
    // Lots of code removed
}

-(void)loadGoogle {
    string = @"http://google.com";  
    NSURL *url = [NSURL URLWithString: string];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requestObj];
}
于 2012-07-11T01:15:12.780 回答