从Web加载NSString时显示UIActivityIndi​​catorView

时间:2011-07-06 11:00:58

标签: xcode download nsurl uiactivityindicatorview

您好,在我的应用中,我使用NSURL从Internet加载NSString,然后标签显示文本。如果我按下按钮加载字符串,它将突出显示并保持高亮显示几秒钟。在此期间,我想要显示UIActivityIndi​​catorView,以通知用户应用程序实际上在做某事。我曾尝试将[activity startAnimating];添加到IBAction,但它只在按钮恢复到默认状态时开始动画,而不是在突出显示时。我也试过了 if ([button state] == UIControlStateHighlited) { [activity startAnimating]; } 但它不起作用。


很棒,现在可以了!非常感谢!你忘了将[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];

}

1 个答案:

答案 0 :(得分:1)

我认为你应该重写你的代码。也许我会为你做的。 :)

首先,下载[ASIHTTPRequest库] [1]。它是处理网络文件的绝佳库。我认为您应该使用队列

然后将此代码放入视图控制器中:

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

}

在头文件中声明theese对象:

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

我认为它会起作用。 ;)


编辑:我更新了代码,因此必须正常工作。我亲自检查过。我加载数据的方法更快,更安全,更高效。