进度条无规律地移动
Posted
技术标签:
【中文标题】进度条无规律地移动【英文标题】:progressBar moving irrevently 【发布时间】:2013-05-16 10:12:56 【问题描述】:我正在使用进度条来显示上传到 ftpserver 的文件的上传百分比,这是它第一次很好地更新,当我取消上传并再次启动时。 50% 的上传进度条正在填充,第三次为 25% 的总进度条正在填充。
这里是代码(我使用 s7ftp 类上传到服务器)
- (void)uploadBytesWritten:(S7FTPRequest *)request
if (uploadedData < totalFileSize)
uploadedData = uploadedData + request.bytesWritten;
float total = (float)totalFileSize;
float bytes = (float)request.bytesWritten;
float pt = (bytes/total);
float totalSizeMb = (total/1048576);
// NSLog(@"File Size in Mb:%f",totalSizeMb);
float uploadDataMb = (uploadedData/1048576);
NSString* totalSize = [NSString stringWithFormat: @"%5.1f", totalSizeMb];
NSString* uploadData = [NSString stringWithFormat: @"%5.1f", uploadDataMb];
// float str = (uploadDataMb/totalSizeMb);
float str = (uploadedData/total);
float string = str*100;
int PercentageFinal = roundf(string);
NSString *uploadPercentage = [NSString stringWithFormat:@"%d",PercentageFinal];
uploadpercentage = [NSString stringWithFormat:@"%@Mb/%@Mb", uploadData, totalSize];
NSLog(@"Uploading percentage:%@",uploadpercentage);
NSString *percentage = [NSString stringWithFormat:@"%f",pt];
uploadProgressView.progress += [percentage doubleValue];
[[NSNotificationCenter defaultCenter] postNotificationName:@"UpdatePercentageNotification" object:nil userInfo:[[NSDictionary alloc] initWithObjectsAndKeys:percentage, @"percentage",uploadpercentage,@"UploadPercentage",uploadPercentage,@"Percent",nil]];
在另一个班级
-(void)Viewdidload
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updatePercentageNotificationOne:) name:@"UpdatePercentageNotification" object:nil];
- (void)updatePercentageNotificationOne:(NSNotification *)notification
NSLog(@"Updating percentage in upload all view");
// NSString *uploadpercentage = [[notification userInfo ]objectForKey:@"UploadPercentage"];
// NSLog(@"Percentage:%@",uploadpercentage);
NSString *percent= [[notification userInfo]objectForKey:@"Percent"];
NSLog(@"percent:%@",percent);
NSString *percentage = [[notification userInfo] objectForKey:@"percentage"];
// NSLog(@"Percentage......%@",percentage);
percentageLabel.text = percent;
ProgressBar.progress +=[percentage doubleValue];
[[NSNotificationCenter defaultCenter] removeObserver:@"UpdatePercentageNotification"];
【问题讨论】:
你在哪里为进度条分配了初始化 【参考方案1】:K 当您再次开始上传时,progressBar 需要重置。当用户点击上传时,您只需分配初始化它。
【讨论】:
那么可能是其他一些值,如totalFileSize,uploadData 没有正确初始化。以上是关于进度条无规律地移动的主要内容,如果未能解决你的问题,请参考以下文章