从 Web 加载 NSString 时显示 UIActivityIndicatorView
您好,在我的应用程序中,我使用 NSURL 从互联网加载 NSString,然后标签显示文本。如果我按下按钮加载字符串,它就会突出显示并保持突出显示几秒钟。在那段时间里,我希望显示一个 UIActivityIndicatorView,以通知用户该应用程序实际上正在执行某些操作。我尝试过将 [activity startAnimating];
添加到 IBAction,但它仅在按钮返回默认状态时开始动画,而不是在突出显示时开始动画。我也尝试过 if ([按钮状态] == UIControlStateHighlited) { [活动开始动画]; }
但它不起作用。
太棒了,现在可以了!多谢!您忘记将 [spinner start animating] 放入代码中:D。有一个错误,如果您连续多次按下该按钮,应用程序就会崩溃,因此它被删除了:
- (IBAction)load:(id)sender {
if ([act isAnimating]) {
}
else {
ASINetworkQueue *queue = [ASINetworkQueue queue];
ASIHTTPRequest *usdRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADUSD=X&f=l1"]];
ASIHTTPRequest *eurRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADEUR=X&f=l1"]];
ASIHTTPRequest *dateRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADEUR=X&f=d1"]];
ASIHTTPRequest *timeRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADEUR=X&f=t1"]];
[queue addOperation:usdRequest];
[queue addOperation:eurRequest];
[queue addOperation:dateRequest];
[queue addOperation:timeRequest];
[queue setQueueDidFinishSelector:@selector(parseLoadedData:)];
[queue setRequestDidFinishSelector:@selector(requestLoaded:)];
[queue setDelegate:self];
[queue go];
[act startAnimating];
}
}
- (void)requestLoaded:(ASIHTTPRequest *)request {
if([[request url] isEqual:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADUSD=X&f=l1"]]) {
usdString = [[request responseString] retain];
} else if([[request url] isEqual:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADEUR=X&f=l1"]]) {
eurString = [[request responseString] retain];
} else if([[request url] isEqual:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADEUR=X&f=d1"]]) {
dateString = [[request responseString] retain];
} else if([[request url] isEqual:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADEUR=X&f=t1"]]) {
timeString = [[request responseString] retain];
}
}
- (void)parseLoadedData:(ASIHTTPRequest *)request {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *Date = [dateString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
NSString *Time = [timeString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
NSString *total = [NSString stringWithFormat:@"%@ %@",Date,Time];
if ([eurString length] == 0) {
test.text = [defaults objectForKey:@"date"];
eur.text = [defaults objectForKey:@"eur"];
usd.text = [defaults objectForKey:@"usd"];
} else {
test.text = total;
eur.text = eurString;
usd.text = usdString;
[defaults setObject:test.text forKey:@"date"];
[defaults setObject:usd.text forKey:@"usd"];
[defaults setObject:eur.text forKey:@"eur"];
}
[defaults synchronize];
[eurString release];
[usdString release];
[dateString release];
[timeString release];
[act stopAnimating];
}
Hi in my app I load a NSString from Internet using NSURL, and then a label shows the text. If i press the button to load the String, it becomes highlighted and stays highlighted for a couple of seconds. During that time i want a UIActivityIndicatorView to show up, to inform the user that the app is actually doing something. Ive tried just adding [activity startAnimating];
to the IBAction but it only starts animating when the button is back to the default state, not when its highlighted. ive also triedif ([button state] == UIControlStateHighlited) {
[activity startAnimating];
}
but it doesn't work.
awesome, now it works! Thanks a lot! You forgot to put [spinner start animating] into the code :D. There was a bug that if you pressed the button many times in a row the app would crash, so it got rid of it:
- (IBAction)load:(id)sender {
if ([act isAnimating]) {
}
else {
ASINetworkQueue *queue = [ASINetworkQueue queue];
ASIHTTPRequest *usdRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADUSD=X&f=l1"]];
ASIHTTPRequest *eurRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADEUR=X&f=l1"]];
ASIHTTPRequest *dateRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADEUR=X&f=d1"]];
ASIHTTPRequest *timeRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADEUR=X&f=t1"]];
[queue addOperation:usdRequest];
[queue addOperation:eurRequest];
[queue addOperation:dateRequest];
[queue addOperation:timeRequest];
[queue setQueueDidFinishSelector:@selector(parseLoadedData:)];
[queue setRequestDidFinishSelector:@selector(requestLoaded:)];
[queue setDelegate:self];
[queue go];
[act startAnimating];
}
}
- (void)requestLoaded:(ASIHTTPRequest *)request {
if([[request url] isEqual:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADUSD=X&f=l1"]]) {
usdString = [[request responseString] retain];
} else if([[request url] isEqual:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADEUR=X&f=l1"]]) {
eurString = [[request responseString] retain];
} else if([[request url] isEqual:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADEUR=X&f=d1"]]) {
dateString = [[request responseString] retain];
} else if([[request url] isEqual:[NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=CADEUR=X&f=t1"]]) {
timeString = [[request responseString] retain];
}
}
- (void)parseLoadedData:(ASIHTTPRequest *)request {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *Date = [dateString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
NSString *Time = [timeString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
NSString *total = [NSString stringWithFormat:@"%@ %@",Date,Time];
if ([eurString length] == 0) {
test.text = [defaults objectForKey:@"date"];
eur.text = [defaults objectForKey:@"eur"];
usd.text = [defaults objectForKey:@"usd"];
} else {
test.text = total;
eur.text = eurString;
usd.text = usdString;
[defaults setObject:test.text forKey:@"date"];
[defaults setObject:usd.text forKey:@"usd"];
[defaults setObject:eur.text forKey:@"eur"];
}
[defaults synchronize];
[eurString release];
[usdString release];
[dateString release];
[timeString release];
[act stopAnimating];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你应该重写你的代码。也许我会为你做。 :)
首先,下载[ASIHTTPRequest 库][1]。它是处理网络文件的绝佳库。我认为你应该为此使用队列。
然后将此代码放入您的视图控制器中:
在您的头文件中声明这些对象:
我认为它会起作用。 ;)
编辑:我更新了代码,因此它必须有效。我自己查了一下。我加载数据的方法更快、更安全、更高效。
I think that you should rewrite your code. Maybe I'll do it for you. :)
First of all, download [ASIHTTPRequest library][1]. It's great library for working with network files. I think that you should use queue for this.
Then put this code in your view controller:
In your header file declare theese objects:
I think that it will work. ;)
EDIT: I updated the code so that is must work. I checked it myself. My method of loading your data is faster, safer and more efficient.