NSURLConnection的代表不工作

问题描述:

我已经搜索并尝试了很多,但在我的应用程序(它使用的是iOS 5 SDK)中的NSURLConnection的委托方法被调用只有当我初始化,并开始在viewDidLoad方法或任何按钮的点击事件的连接。NSURLConnection的代表不工作

但我想我的解析JSON文件后调用它。为JSON中的每条记录创建一个新的连接。

谁能告诉我发生了什么?东西在IOS SDK 5

这里改变的是我的代码:

-(void)getData:(NSString*)URL{ 
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:URL]]; 

shortURL = [NSString stringWithString:URL]; 


connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self]; 

//NSAssert(connection!=nil, @"no connection", nil); 
[connection start]; 

}

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response 

{ // application specific stuff }

该功能的getData是写在一个类继承来自NSObject。它被称为

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

UITableView的方法。

请帮我这个。

+2

发布您的代码... – esqew 2012-01-27 16:02:23

+1

最后委托方法在iOS 5中读depricated在文档 – 2012-01-27 16:09:11

+0

请在这里提供的代码块,因此,实际的问题可以鉴定。 – iCreative 2012-01-27 16:22:30

假设JSON解析代码在后台线程发生的事情,你必须为这个线程创建一个NSRunLoop。

的NSURLConnection的类只是自身增加了runloop,如果不存在,没有任何加工或委托方法将被调用。

connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self]; 
[[NSRunLoop currentRunLoop] run]; 

注:run方法将阻止