如何在加载连接后将数据从模型传递到视图控制器?

时间:2015-06-04 20:32:54

标签: ios objective-c

我正在为应用程序的登录功能工作,我在连接后从服务器收到一本字典。我在函数中收到字典 - (void)connectionDidFinishLoading:(NSURLConnection *)连接 我在我的模型中有。使用从中收到的字典信息,然后我想将该字典发送到视图控制器,但该函数已经在connectionDidFinishLoading完成时返回。

如何将数据传回视图控制器?

1 个答案:

答案 0 :(得分:0)

您需要设置teh委托并实现回调方法

NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:theURL];
    NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:NO];
    [urlConnection start];

delegate methods

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [_detailsResponse setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [_detailsResponse appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"Error: %@, %@", [error localizedDescription], [error localizedFailureReason]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // Do what you want with the data here.
}