我想异步调用一个方法。这是一种从服务器获取 HTML 并将其设置为 UIWebView 的方法:

NSString *htmlTest = [BackendProxy getContent];
[webView loadHTMLString:htmlTest baseURL: nil];
[webView setUserInteractionEnabled:YES];

我想在数据获取期间在 UIWebView 中启动一个事件指示器,所以我需要异步调用 getContent。我怎样才能做到这一点?

最佳答案

我建议 performSelectorInBackground:withObject:NSObject

像下面这样:

- (void)loadIntoWebView: (id) dummy
{

    NSString *html = [BackendProxy getContent];
   [self performSelectorOnMainThread: @selector(loadingFinished:) withObject: html];
}


- (void)loadingFinished: (NSString*) html
{
   // stop activity indicator
   [webView loadHTMLString:html baseURL: nil];
   [webView setUserInteractionEnabled:YES];
}

- (void) foo
{
   // ...
   // start activity indicator
   [self performSelectorInBackground: @selector(loadIntoWebView:) withObject: nil];
}

10-07 21:24