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];
}

我正在使用 iOS 10.6.7 的 Mac Mini

4

5 回答 5

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

于 2011-09-05T08:52:14.843 回答
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];
}
于 2011-09-06T08:31:56.080 回答
4

对于同步使用:(错误的形式,但有效的解决方案)

//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];
}
于 2011-09-06T08:37:17.530 回答
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];
于 2011-09-05T08:51:43.897 回答
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];

最终完成后,从视图中移除微调器并释放。

于 2011-09-05T09:09:28.193 回答