XCode-未加载UIWebView

XCode-未加载UIWebView,xcode,uiwebview,Xcode,Uiwebview,我在Resources文件夹中有两个本地.html文件。我试着用下面的方法加载它们,但只加载最后一页。我做错了什么 File=please\u wait.html 这个不行 NSError *error; NSString* path = [[NSBundle mainBundle] pathForResource:@"please_wait" ofType:@"html"]; NSString* htmlString = [NSString stringWithContentsOfFile:p

我在Resources文件夹中有两个本地.html文件。我试着用下面的方法加载它们,但只加载最后一页。我做错了什么

File=please\u 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.
File=update\u 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];

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

要解决此问题,请将方法分为三部分,
A
B
C
,并将
A
中的
UIWebView
委托设置为在
webviewdiffinishload:
上调用
B
,将
B
中的委托设置为调用
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

你确定“please_wait.html”在你的项目中,或者可能是某个表单的拼写错误!是的。如果我试图通过注释出它们工作的其他内容来访问htmlString部分中的任何一个,那么当它们与其他代码结合时,就会被跳过。(不仅仅是速度太快,我没有注意到。我添加了断点,它们仍然不会出现,除非它们是唯一的代码)那么有没有办法告诉我的代码“等待”直到刷新页面,然后继续?是的。您需要实现
-(void)webViewDidFinishLoad:(UIWebView*)webView
,作为
UIWebViewDelegate
消息的一部分。编辑:如果加载后应该执行的代码需要未存储在类中的上下文,我将使用块。例如,将退出代码放入块中,将其传递给webview控制器类,将其另存为其成员之一,然后执行
self.myExitCodeBlock()
inside
webViewDidFinishLoad
@mrhappyathma您的函数需要退出,并让主循环“发挥其魔力”。但是你可以像我在回答的第二部分中描述的那样,通过在方法的三个部分之间传递代表来说明你的意思吗?我对可可触感的东西还很陌生,我不知道该怎么做。
-(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];
}