Show UIActivityIndicatorView when loading NSString from Web

人盡茶涼 提交于 2020-01-17 03:17:22

问题


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 tried if ([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];

}

回答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:

- (IBAction)buttonClicked:(id)sender {

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

}

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

    [yourSpinner stopAnimating];

}

In your header file declare theese objects:

NSString *usdString;
NSString *eurString;
NSString *dateString;
NSString *timeString;

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.



来源:https://stackoverflow.com/questions/6595315/show-uiactivityindicatorview-when-loading-nsstring-from-web

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!