按下按钮时显示活动指示器动画
我有一个名为“同步”的按钮。当点击/按下时,它会将数据发送到网络服务器并由服务器检索答案(这需要一段时间并冻结 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 上工作
I have a button called "Sync". When tapped / pressed it sends data to a webserver and retrieves the answer by the server (what takes a while and freezes the gui / button) so how do I add a "Please wait" and activity indicator until the data / answer from the server arrives and the text / indicator is deleted and replaced by the answer from the server?
This code freezes my app:
- (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];
}
i am working on a Mac Mini with iOS 10.6.7
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 UIAleartView 中添加活动指示器会有所帮助。
看看这个...
编辑:
您的应用程序被阻止的原因可能是这个方法
sendSynchronousRequest:(NSURLRequest *)request
以下是该方法的描述。
如果您不希望应用程序阻塞,请从其他线程调用它。也请阅读以下内容。
请参考 NSURLConnection 的类参考。上述两个块均取自该块。
http://developer.apple.com/library /mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html
Adding an activity indicator in UIAleartView will help.
Checkout this...
EDIT:
Probably the reason why your application is getting blocked is this method
sendSynchronousRequest:(NSURLRequest *)request
Following is the description of the method.
If you don't want you application to block then call this from other thread. Read following too.
Please refer to Class reference of NSURLConnection. Both of above blocks are taken from that.
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html
您的同步请求会阻塞 UI 线程,这就是活动指示器无法启动的原因。出于多种原因,您应该异步使用 NSURLConnection,这就是其中之一。
而不是:
执行:
然后创建 returnData 实例变量并实现 NSURLConnectionDeletgate 方法:
Your synchronous request blocks the UI thread and that's why activity indicator will not start. You should use NSURLConnection asynchronously for multiple reasons, and that's one of them.
Instead of:
Do:
Then make returnData instance variable and implement NSURLConnectionDeletgate methods:
对于同步使用:(形式不好,但可行的解决方案)
For synchroneous use: (bad form, but working solution)
创建:
开始:
停止:
最终完成后,从视图中删除微调器并释放。
Create:
Start:
Stop:
When you're finally done, remove the spinner from the view and release.