0

我已經搜索並嘗試了很多,但在我的應用程序(它使用的是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

回答

2

假設JSON解析代碼在後臺線程發生的事情,你必須爲這個線程創建一個NSRunLoop。

的NSURLConnection的類只是自身增加了runloop,如果不存在,沒有任何加工或委託方法將被調用。

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

注:run方法將阻止