gpt4 book ai didi

iphone - iOS - UIProgressView 只更新一次

转载 作者:可可西里 更新时间:2023-11-01 06:23:37 26 4
gpt4 key购买 nike

我正在从 API 加载数据,并使用 UIProgressView 显示已加载的数据量。

在我的 viewWillAppear 中,我使用可达性来检查是否存在互联网连接。然后,如果有,则在函数中调用以下行 10 次。

[self performSelectorInBackground:@selector(updateProgress) withObject:nil];

然后运行这个方法

-(void)updateProgress {
float currentProgress = myProgressBar.progress;
NSLog(@"%0.2f", currentProgress);
[loadingProg setProgress:currentProgress+0.1 animated:YES];
}

float 增加 0.1 并且加载 View 显示它。

当 View 被关闭(它是模态视图)然后被召回时,该方法运行并且 NSLog 显示 currentProgress 正在按预期递增。但是,进度条仍然是空的。有谁知道是什么原因造成的?

作为引用,我正在使用 ARC。

更新:

这就是我调用 API 的方式

NSString *urlString = **url**;
NSURL *JSONURL = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:JSONURL
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:10];
if(connectionInProgress) {
[connectionInProgress cancel];
}
connectionInProgress = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

//This is where I call the update to the progress view

我有这些功能:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
JSONData = [NSMutableData data];
[JSONData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[JSONData appendData:data];
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
//add data to mutable array and other things
}

最佳答案

当您处理用户界面 (UI) 组件时,您必须在主线程中执行这些方法。作为一个规则,当你编程时,你需要在主线程中设置 UI 操作,并在后台线程中设置繁重、复杂和对性能要求更高的操作——这称为多线程(作为附带建议,阅读 GCD 会很好——Grand中央调度。如果你需要做更长的操作,check this good tutorial from Ray Wenderlich .)

要解决这个问题,您应该调用 [self performSelectorOnMainThread:@selector(updateProgress) withObject:nil];,然后在方法中执行以下操作:

-(void)updateProgress {
float currentProgress = myProgressBar.progress;
NSLog(@"%0.2f", currentProgress);
dispatch_async(dispatch_get_main_queue(), ^{
[loadingProg setProgress:currentProgress+0.1 animated:YES];
});
}

关于iphone - iOS - UIProgressView 只更新一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12235365/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com