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

发布于 2024-11-18 20:03:23 字数 3190 浏览 2 评论 0原文

您好,在我的应用程序中,我使用 NSURL 从互联网加载 NSString,然后标签显示文本。如果我按下按钮加载字符串,它就会突出显示并保持突出显示几秒钟。在那段时间里,我希望显示一个 UIActivityIndi​​catorView,以通知用户该应用程序实际上正在执行某些操作。我尝试过将 [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 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];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洒一地阳光 2024-11-25 20:03:23

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

首先,下载[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];

}

在您的头文件中声明这些对象:

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

我认为它会起作用。 ;)


编辑:我更新了代码,因此它必须有效。我自己查了一下。我加载数据的方法更快、更安全、更高效。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文