2011-04-07 61 views
1

我發送一個請求,並在收到我需要檢查條件的響應,然後導航到下一個屏幕,但是這個條件甚至在我收到響應之前檢查。我如何才能在收到回覆後才能檢查狀況。提前致謝。檢查條件後的條件

回答

0

你使用NSURL連接還是任何組件?

嘗試使用NSURL連接委託。看看這個鏈接:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html#//apple_ref/occ/cl/NSURLConnection

我認爲這兩種方法中的任何一種都可以。
- (無效)連接:(NSURLConnection的*)連接didReceiveData:(NSData的*)數據或
- (無效)連接:(NSURLConnection的*)連接didReceiveResponse:(NSURLResponse *)響應

+0

我使用NSURL連接它的使用NSURL連接代表 – 2011-04-07 15:38:21

+1

嘗試異步請求。看看這個鏈接:http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html%23//apple_ref/occ/cl/NSURLConnection。我認爲這兩種方法都有效。 - (void)連接:(NSURLConnection *)連接didReceiveData:(NSData *)數據或 - (void)連接:(NSURLConnection *)連接didReceiveResponse:(NSURLResponse *)響應 – insomiac 2011-04-07 15:44:15

0

我會考慮使用ASI庫。比NSURLConnection更好用。

無論採用哪種方式,都應該異步啓動請求並將委託設置爲self。

當您在委託回調中收到回覆時,您可以繼續進入下一個屏幕。

對於ASI你會怎麼做?

- (void)dorequest 
{ 
    NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"]; 
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
    [request setDelegate:self]; 
    [request startAsynchronous]; 

    // Do no do anything else in this method. 
    // wait for the response to call your delegate method 
} 

- (void)requestFinished:(ASIHTTPRequest *)request 
{ 
    [self loadNextScreen]; 
} 

NSURLConnection將使用類似的代碼。只要確保你使用異步方法。

0

我覺得這個問題沒有正確解釋,因爲它被誤解了。我使用的NSURLConnection和請求是一個異步的,所以這不是問題。我需要一些代碼才能在數據下載完成後執行。

我得到了答案;通過使用NSNotifications。在我的解析器類內connectionDidFinishLoading:(NSURLConnection *)連接方法我發佈通知。在我的viewcontroller類中,我添加了一個觀察者來執行所需的代碼。

如需更多幫助,請參見:http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNotification_Class/Reference/Reference.html

感謝