0

在我的 iPad 应用程序中,我想加载一个带有一些数据的模式窗口。但这些数据可以从 Web 服务调用中检索。因此,我创建了另一个类,并且在该类的 connectionDidFinishLoading 中我可以获得响应数据。由于 Web 服务调用是异步的,我必须等待数据加载模态窗口。谁能帮我一些示例代码?我应该以不同的方式思考吗?

谢谢大家的及时回复。使用 NSNotificationCenter 解决了我的问题。本教程很有帮助http://www.youtube.com/watch?v=WB-QCv_4ANU&feature=plcp

4

2 回答 2

0

Either you can load modal window from connectionDidFinishLoading method. Or you can use delegates to pass data from connectionDidFinishLoading metod to the window that you are going to present. Refer this tutorial.

于 2012-07-22T15:20:22.157 回答
0

您以这种方式开始连接:

NSURL *url = [NSURL URLWithString:<#your url string#>];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
myData = [[NSMutableData alloc] init];
con = [[NSURLConnection alloc] initWithRequest:request delegate:self];

你需要实现NSURLConnectionDelegate委托。

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
 //append data to your NSMutableData object
 [myData appendData: data];
}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{    
    //handle the error
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //here you can use your NSMutableData object, fill your window with the data etc.
    <#your code#>
}

这只是一个例子。您可以在NSURLConnectionDelegate 协议参考中了解更多信息。

于 2012-07-22T15:26:23.527 回答