2011-09-05 114 views
2

我有一個名爲「同步」按鈕。當竊聽/把它壓將數據發送到網絡服務器和服務器(有什麼需要一段時間,並凍結GUI /按鈕)檢索答案讓我怎麼添加一個「請等待」和活動的指標,直到從服務器中的數據/答案到達並且文本/指示符被刪除並由來自服務器的回答取代?顯示活動指示器的動畫時,按下按鈕

此代碼凍結我的應用程序:

- (IBAction) syncButtonTapped 
{ 

[syncButton setEnabled:NO]; 
//resultText.text= @"Bitte warten ..."; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *docDir = [paths objectAtIndex: 0]; 
NSString *docFile = [docDir stringByAppendingPathComponent: @"deck.txt"]; 
NSString *post = [NSString stringWithContentsOfFile:docFile encoding:NSUTF8StringEncoding error:nil]; 


NSString *post2 = [post stringByReplacingOccurrencesOfString:@"\n" withString:@","]; 
NSString *post3 = [post2 stringByReplacingOccurrencesOfString:@"\r" withString:@""]; 
NSString *post4 = [NSString stringWithFormat:@"{\"results\":[%@]}",post3]; 
NSString *urlString = [NSString stringWithFormat:@"http://storecheck.cortona.de/fetchresults.php?results=%@",[post4 stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding]]; 
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
[request setURL:[NSURL URLWithString:urlString]]; 
[request setHTTPMethod:@"GET"]; 
NSString *data4 = @""; 
[data4 writeToFile: docFile atomically: NO encoding: NSUTF8StringEncoding error:nil]; 
//send request & get response 

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
} 

我是一臺Mac Mini的工作與iOS 10.6.7

+0

我還喜歡將黑色背景,0.5 alpha UIView放在一切之前,以使它們變暗並禁用任何與UI元素的用戶交互。 – bdares

+0

輸入被禁用,所以不想這個,不需要這個,你的意思是 –

+0

http://stackoverflow.com/questions/593234/how-to-use-activity-indicator-view-on-iphone – PJR

回答

4

你同步請求阻塞UI線程,這就是爲什麼活動的指標將無法啓動。由於多種原因,您應該異步使用NSURLConnection,這就是其中之一。

相反的:

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 

務必:

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
[conn start]; 
[conn release]; 

然後讓returnData實例變量和實施NSURLConnectionDeletgate方法:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    returnData = [[NSMutableData alloc] init]; 
} 

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

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
    // do whatever you need with your string and stop activity indicator 
    [returnData release]; 
} 
+0

和我怎麼做? –

+0

我已將代碼添加到我的答案中。被檢測的 –

+0

; token before:token on: - (void)connection:(NSURLConnection *)連接didReceiveResponse:(NSURLResponse *)響應 –

1
//Right before the request is made 
UIActivityIndicatorView* indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
[indicatorView setFrame:CGRectMake(0, 0, 16, 16)]; 
[indicatorView setHidesWhenStopped:YES]; 
[indicatorView startAnimating]; 
[self.view addSubview:indicatorView]; 
// Create your request (synchronously or asynchronously) 
... 

// When request is done 
[indicatorView stopAnimating]; 
[indicatorView release]; 
+0

它沒有出現我必須在某處創建視圖或將其添加到標題中?它與以前一樣,當我添加你的代碼? –

+0

@Martin您必須將其添加到您當前的視圖中,使用'addSubView:' – bdares

+0

我已經添加了'setFrame'和'addSubview',以便您有完整的畫面 – apouche

5

添加在UIAleartView活動指標會有所幫助。 結帳這個...

UIAlertView *waitAlert = [[[UIAlertView alloc] initWithTitle:@"Please Wait...." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease]; 

[waitAlert show]; 

UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 

// Adjust the indicator so it is up a few pixels from the bottom of the alert 
indicator.center = CGPointMake(waitAlert.bounds.size.width/2, waitAlert.bounds.size.height - 50); 
[indicator startAnimating]; 
[waitAlert addSubview:indicator]; 
[indicator release]; 

編輯: 可能就是你的應用程序被阻塞的原因是這種方法

sendSynchronousRequest:(NSURLRequest *)request

以下是該方法的描述。

的同步負荷是建立在由類提供異步加載代碼的頂部。調用線程在異步加載系統對專門爲此加載請求生成的線程執行URL加載時被阻塞。爲了執行同步加載,在調用線程中不需要特殊的線程或運行循環配置。

如果你不想讓你的應用程序阻止再從其他線程調用此。請閱讀以下內容。

重要提示:由於此調用有可能需要幾分鐘失敗(使用iOS的蜂窩網絡特別是當),你永遠不應該叫從GUI應用程序的主線程此功能。

請參考NSURLConnection的等級參考。上述兩個塊均取自該塊。

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html

+0

使用未聲明的標識符'waitAlert'? –

+0

在你的代碼中聲明UIAleartView * waitAlert你只是沒有我正在使用的變量。所以在代碼中聲明它們。 – Mohammad

+0

that worked,tahnks,但凍結的問題沒有解決=( –

1

創建:

spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
[spinner setCenter:CGPointMake(kScreenWidth/2.0, kScreenHeight/2.0)]; // I do this because I'm in landscape mode 
[self.view addSubview:spinner]; // spinner is not visible until started 

開始:

[spinner startAnimating]; 

停止:

[spinner stopAnimating]; 

當你最終完成時,從視圖中刪除微調並釋放。

4

對於synchroneous使用:(不好的形式,但工作解決方案)

//Right before the request is made 
UIActivityIndicatorView* indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
[indicatorView setFrame:CGRectMake(0, 0, 16, 16)]; 
[indicatorView setHidesWhenStopped:YES]; 
[indicatorView startAnimating]; 
[self.view addSubview:indicatorView]; 
// Create your request (synchronously or asynchronously) 
[self performSelector:@selector(myDoRequest:) withObject:self afterDelay:0.1]; 
//this gives time for the runloop to complete at least one screen draw.. 
... 

-(void)myDoRequest:(id)sender { 
    // Create your request (synchronously or asynchronously) 
    ... 
    // When request is done 
    [indicatorView stopAnimating]; 
    [indicatorView release]; 
} 
+0

也使用未聲明的標識符。 myDoRequest當我把它變成IBAction爲 –

相關問題