1

Resources フォルダーに 2 つのローカル .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

この最後のものは完全に機能します。Google が表示します。

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

1 に答える 1

3

コメントから、UIWebViewロードは問題なく行われているように見えますが、メソッドを終了するまで、画面上でそれ自体を更新する機会がありません。メソッド内にブレークポイントを設定し、ビューが読み込まれるのを待つだけでは不十分です。iOS がUIWebViewdrawRectメソッドを呼び出す必要があることに気付く前に、メソッドを終了する必要があります。

これを修正するには、メソッドを 3 つの部分に分割し、 A BandCと setUIWebViewのデリゲートAを invoke BonwebViewDidFinishLoad:に設定し、デリゲートBを callに設定します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

ここで、メソッドを 3 つの部分 (最初のページの読み込み、2 番目のページの読み込み、3 番目のページの読み込み) に分割します。

-(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 に答える